Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 鬧鐘定時功能實作筆記

編輯:關於 Android 程式設計

最近參加一個比賽,作品是智能插座助手,需要用到定時觸發功能,於是順手做了一個類似鬧鐘的模塊,鬧鐘資訊用 Android 自帶的 SQLite 資料庫保存。

源碼位址:https://github.com/waterCode/SaveEner.git(專案名 saveenergy)

考慮到有些人沒在用 git,而且代碼還有需要完善和修改的地方,等全部寫完之後我會再上傳一份到 CSDN 檔案存儲。不過學習 git 還是有很多好處,備份代碼就是其一,再也不用壓縮檔傳雲盤了。

專案裡 model 名稱為 saveenergy 的就是這個部分的源碼。大家一起進步,堅持努力,總有一天我們都能成為別人口中的「大神」。有問題歡迎留言。

(一)鬧鐘列表顯示

這裡主要用 LinearLayout 布局,裡面有一個 ListView 和一個 ImageButton。幾點個人覺得要注意的:

1. ImageButton 自帶灰色背景,textColor 要設成透明。

2. 我用的是綠色背景,ListView 的 item 布局裡 TextView 是白色字體;為了方便調試,可以用 tools:textColor 只在預覽中生效。

3. ListView 記得設定 divider 與 showDividers,這裡用了一張自訂的分隔線圖:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:divider="@drawable/my_divider"
    android:showDividers="beginning|end">
</LinearLayout>

ListView 與 item 的具體代碼可以到我的 GitHub 下載查看。

Activity 這邊要做的事:

1. 初始化布局,拿到各個 View 的實例。

2. 拿到資料庫指標,為 ListView 設定 adapter。這裡用的是 SimpleCursorAdapter,要注意它構造函數最後一個 Flag 參數。這一頁的核心就是 SimpleCursorAdapter 的設定,因為它主要負責和資料類互動;如果想鍛鍊自己,也可以繼承 BaseAdapter 自己封裝,看個人喜好。

3. 為 ListView 和 Button 設定監聽,刷新列表用的是:

cursorAdapter.notifyDataSetChanged();

(二)鬧鐘編輯頁面

布局部分:上方是簡單的 TextView 和 ImageButton,接著是 TimePicker,然後是一個 ListView。小透劇一下,這個 ListView 用的是 SimpleAdapter,點擊後會彈出一個選擇是否啟用的 Dialog,這裡用的是 AlertDialog。

需要注意的地方:

1. TimePicker 的外觀可能因系統而異。預設在某個版本上是這樣:

但同一份代碼在我的華為 P7 上卻長這樣,布局反差極大,一開始讓我這菜鳥措手不及,問了幾個寫安卓的朋友也沒人說得清,最後在 Stack Overflow 找到答案。加上這一句就能讓它變回「麵條狀」:

android:timePickerMode="spinner"

代碼部分:

1. 初始化各個控制項,取得實例。

2. 用 SimpleAdapter 為 ListView 設定資料。

3. 設定各個監聽。

4. 結束時把資訊存進資料庫,並在系統中註冊鬧鐘。

這裡要特別注意 PendingIntent 的第二個參數。為了能區分多個鬧鐘,我用的是鬧鐘資訊在資料庫中的 id:

int id = dbOperator.getAlarmId(time);   // 取得新加入鬧鐘在資料庫的 id
Intent intent = new Intent(this, AlarmReceiver.class);
intent.putExtra("_id", id);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.getAlarmTime().getTimeInMillis(), pendingIntent);

(三)刪除鬧鐘

這一頁由長按鬧鐘進入,只有一個 ListView,用的還是 SimpleCursorAdapter。這裡要注意重寫 bindView(),因為點擊刪除按鈕時必須能監聽到事件:

@Override
public void bindView(View view, final Context context, final Cursor cursor) {
    super.bindView(view, context, cursor);
    final int id = cursor.getInt(0);
    ImageButton imageButton = (ImageButton) view.findViewById(R.id.alarm_delete_button);
    imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatabaseOperator operator = new DatabaseOperator(context);
            operator.delete(id);
            cursor.requery();
            adapter.notifyDataSetChanged();
        }
    });
}

資料庫那部分這裡不多講,可以參考代碼。

基本上要注意的就是這些。我只貼出認為關鍵的代碼,並把當時踩過的坑以「注意」的形式寫出來。完整源碼可以在文章開頭的 GitHub 位址下載。因為這只是定時功能的一部分,怕大家下載後找不到位置,這裡附上相關檔案結構的截圖:

有關資料庫操作的類:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved