任务终止后的AlarmManager

时间:2011-06-22 01:19:35

标签: android broadcastreceiver 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永远不会被触发。我能做些什么吗?

1 个答案:

答案 0 :(得分:2)

在清单中而不是在代码中注册广播接收器的意图过滤器。