0
SilenceReceiver silenceReceiver = new SilenceReceiver();
IntentFilter silenceFilter = new IntentFilter(SilenceReceiver.SILENCE_ALARM);
registerReceiver(silenceReceiver, silenceFilter);
AwakeReceiver awakeReceiver = new AwakeReceiver();
IntentFilter awakeFilter = new IntentFilter(AwakeReceiver.WAKE_ALARM);
registerReceiver(awakeReceiver, awakeFilter);
GregorianCalendar sleepCalendar = new GregorianCalendar();
sleepCalendar.set(Calendar.SECOND, sleepCalendar.get(Calendar.SECOND)+5);
long triggerTime = sleepCalendar.getTimeInMillis();
GregorianCalendar awakeCalendar = new GregorianCalendar();
awakeCalendar.set(Calendar.SECOND, awakeCalendar.get(Calendar.SECOND)+30);
long awakeTime = awakeCalendar.getTimeInMillis();
AlarmManager alarm = (AlarmManager) this.getSystemService(Service.ALARM_SERVICE);
PendingIntent sleepIntent = PendingIntent.getBroadcast(this, 0, new Intent(SilenceReceiver.SILENCE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, triggerTime, sleepIntent);
PendingIntent awakeIntent = PendingIntent.getBroadcast(this, 0, new Intent(AwakeReceiver.WAKE_ALARM), 0);
alarm.set(AlarmManager.RTC_WAKEUP, awakeTime, awakeIntent);
問題是如果任務被殺死,我的AwakeReceiver永遠不會觸發。我能做些什麼嗎?任務中止後的AlarmManager
謝謝。有沒有辦法在代碼中這樣做,以便我可以動態註冊/取消註冊它們?或者是否註冊了未使用的接收器而不對電池徵稅? – user123321
它爲什麼不同? (我知道它的確如此,但爲什麼/如何?)在清單中註冊時是否有另一種上下文?如果您在清單中註冊,爲什麼無法註銷代碼? 更重要的是,如果您必須註冊一個像ACTION_SCREEN_OFF這樣的操作,那麼必須在代碼中完成操作,您該怎麼辦?你怎麼能註冊它的行爲就像它在清單中註冊一樣? – IBoS