任务终止后的 AlarmManager
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 永远不会触发。我能做些什么吗?
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);
The problem is if the task is killed my AwakeReceiver never gets trigger. Is there anything I can do about this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在清单中而不是在代码中注册广播接收器的意图过滤器。
Register the intent filters for the broadcast receiver in the manifest instead of in code.