|
本篇使用AlarmManager達到準時執行某程式,跟鬧鐘的方式一樣,我之前用過IntentService、Service、Handler,都不能長期使用。
Servicer簡單缺點整理:
IntentService:只要關閉activity背景保存視窗,IntentService背景所執行的程式就被強迫結束,好像可以重開(我沒試過)。
Service、Handler:雖然關閉activity會繼續執行,但他是重新在開啟一次service,這到還好,但當手機螢幕關閉處於待機狀態時,service會停止運作暫停在那邊,等你喚醒再繼續執行,所以用這個加timer做定時通知會整個亂掉,timer會停下來不數了。
AlarmManager主要架構是
跟系統註冊鬧鐘甚麼時間點 → 時間到系統會發送一個鬧鈴 → 你的activity接收那個鬧鈴後執行你的程式
當你註冊好幾個鬧鐘已經晚於現在時間,他會馬上通通都執行完
先建立接收鬧鈴的程式
在Manifest.xml 中加入接收系統鬧鈴
- <application>
- <activity
- android:name=".MainActivity">
- </activity>
- <!-- 當鬧鈴時間到達時要執行的程式 -->
- <receiver android:name=".AlarmReceiver">
- <intent-filter>
- <action android:name="activity_app" />
- </intent-filter>
- </receiver>
- </application>
複製代碼
建立一個class檔,執行接收到鬧鈴後的程式
AlarmManager AlarmReceiver receiver
- public class AlarmReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
-
- Bundle bData = intent.getExtras();
- if(bData.get("title").equals("activity_app"))
- {
- //主要執行的程式
- }
- }
- }
- 註冊和取消鬧鐘
- /*** 加入(與系統註冊)鬧鐘 ***/
- public static void add_alarm(Context context, Calendar cal) {
- Log.d(TAG, "alarm add time: " + String.valueOf(cal.get(Calendar.MONTH)) + "." + String.valueOf(cal.get(Calendar.DATE)) + " " + String.valueOf(cal.get(Calendar.HOUR_OF_DAY)) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
- Intent intent = new Intent(context, AlarmReceiver.class);
- // 以日期字串組出不同的 category 以添加多個鬧鐘
- intent.addCategory("ID." + String.valueOf(cal.get(Calendar.MONTH)) + "." + String.valueOf(cal.get(Calendar.DATE)) + "-" + String.valueOf((cal.get(Calendar.HOUR_OF_DAY) )) + "." + String.valueOf(cal.get(Calendar.MINUTE)) + "." + String.valueOf(cal.get(Calendar.SECOND)));
- String AlarmTimeTag = "Alarmtime " + String.valueOf(cal.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(cal.get(Calendar.MINUTE)) + ":" + String.valueOf(cal.get(Calendar.SECOND));
- intent.putExtra("title", "activity_app");
- intent.putExtra("time", AlarmTimeTag);
- PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
- am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi); //註冊鬧鐘
- }
- /*** 取消(與系統註冊的)鬧鐘 ***/
- private static void cancel_alarm(Context context, Calendar cal) {
- Log.d(TAG, "alarm cancel time: " + String.valueOf(cal.get(Calendar.MONTH)) + "." + String.valueOf(cal.get(Calendar.DATE)) + " " + String.valueOf(cal.get(Calendar.HOUR_OF_DAY)) + ":" + cal.get(Calendar.MINUTE) + ":" + cal.get(Calendar.SECOND));
- Intent intent = new Intent(context, AlarmReceiver.class);
- // 以日期字串組出不同的 category 以添加多個鬧鐘
- intent.addCategory("ID." + String.valueOf(cal.get(Calendar.MONTH)) + "." + String.valueOf(cal.get(Calendar.DATE)) + "-" + String.valueOf((cal.get(Calendar.HOUR_OF_DAY) )) + "." + String.valueOf(cal.get(Calendar.MINUTE)) + "." + String.valueOf(cal.get(Calendar.SECOND)));
- String AlarmTimeTag = "Alarmtime " + String.valueOf(cal.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(cal.get(Calendar.MINUTE)) + ":" + String.valueOf(cal.get(Calendar.SECOND));
- intent.putExtra("title", "activity_app");
- intent.putExtra("time", AlarmTimeTag);
- PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
- am.cancel(pi); //取消鬧鐘,只差在這裡
- }
複製代碼
當 AlarmManager 執行 set() 時,Android 系統會比對已註冊的其他 Intent 的 action、data、type、class、category,如果這幾個屬性完全相同,則系統會將這兩個 Intent 視為一樣,這時系統會視 PendingIntent.FLAG???? 參數以決定如何處理這個新註冊的 Intent。原文
如何呼叫此函式
- //下一分鐘0秒時
- Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+8:00")); //取得時間
- cal.add(Calendar.MINUTE, 1); //加一分鐘
- cal.set(Calendar.SECOND, 0); //設定秒數為0
- add_alarm(context, cal); //註冊鬧鐘
- //在MainActivity中 context = this
- //在service中 context = context(service的)
複製代碼- //註冊多個,每分鐘響共10次
- Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+8:00")); //取得時間
- for(int i = 0; i < 10; i++){
- cal.add(Calendar.MINUTE, 1); //加一分鐘
- cal.set(Calendar.SECOND, 0); //設定秒數為0
- add_alarm(context, cal); //註冊鬧鐘
- }
- //在MainActivity中 context = this
- //在service中 context = context(service的)
複製代碼 也可以在AlarmReceiver 中註冊下一個鬧鐘,也就是說當第一個響起,註冊下一個
如果要每間隔一個小時發出提醒
改寫
- public static void add_alarm(Context context) {
- final long ALARM_TRIGGER_AT_TIME = SystemClock.elapsedRealtime() + 10000;
- final long ALARM_INTERVAL = 1000 * 60 * 60 ;
- Intent intent = new Intent(context, AlarmReceiver.class);
- intent.putExtra("title", "activity_app");
- PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
- AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
- am.setRepeating(AlarmManager.RTC_WAKEUP,ALARM_TRIGGER_AT_TIME, ALARM_INTERVAL,pi); //註冊鬧鐘
- }
複製代碼
參考文章
https://xiang1023.blogspot.com/2017/11/android-alarmmanager.html
|
|