AlarmManager 怎樣在程序被幹掉的情況下觸發回撥
阿新 • • 發佈:2018-12-23
我用 AlarmManager.setRepeating 設定了一個重複的回撥,程序還在的時候能正常呼叫。
把程序殺掉後,這個定時器就不會被觸發了,要怎樣設定才能保證定時器會被觸發
// 啟動程式碼
void InitAlarm()
{
Intent intent = new Intent(this, AlarmReceiver2.class);
// Create a PendingIntent to be triggered when the alarm goes off
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 10086,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 5000; // 5 seconds
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Log.d("test", "start time alarm");
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, alarmPendingIntent);
}
// 處理的回撥程式碼
//<receiver android:process=":remote_notification" android:name="AlarmReceiver"></receiver>
@Override
public void onReceive(Context context, Intent intent) {
Log.d("", "Alarm Recieved!");
Toast.makeText(context, "onReceive click", Toast.LENGTH_SHORT).show();
}
把程序殺掉後,這個定時器就不會被觸發了,要怎樣設定才能保證定時器會被觸發
// 啟動程式碼
void InitAlarm()
{
Intent intent = new Intent(this, AlarmReceiver2.class);
// Create a PendingIntent to be triggered when the alarm goes off
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(MainActivity.this, 10086,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup periodic alarm every 5 seconds
long firstMillis = System.currentTimeMillis(); // first run of alarm is immediate
int intervalMillis = 5000; // 5 seconds
AlarmManager alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Log.d("test", "start time alarm");
alarm.setRepeating(AlarmManager.RTC_WAKEUP, firstMillis, intervalMillis, alarmPendingIntent);
}
// 處理的回撥程式碼
//<receiver android:process=":remote_notification" android:name="AlarmReceiver"></receiver>
@Override
public void onReceive(Context context, Intent intent) {
Log.d("", "Alarm Recieved!");
Toast.makeText(context, "onReceive click", Toast.LENGTH_SHORT).show();
}