我有一个调用 AlarmManager 的应用程序
Intent intent;
intent = new Intent(context, MyEventReceiver.class);
PendingIntent appIntent = PendingIntent.getBroadcast(context, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
appIntent);
在舱单中我有强制性条目
<receiver android:name=".MyEventReceiver"
android:process=":remote" />
MyEventReceiver 看起来像这样
public class MyEventReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
try
{
// DO SOME WORK
}
catch (Exception e)
{
Log.e("MyEventReceiver", e.getMessage().toString());
}
}
}
当警报被激活时,即使我的应用程序没有运行,MyEventReceiver 也应该启动并执行某些操作。在模拟器中是这种情况,但在实际设备上情况并非如此。
作为示例,我将在模拟器上启动 MyApplication,在 DDMS 中我可以看到 MyApplication 正在运行的进程。我从 MyApplication 中添加一个警报,然后在 DDMS 中终止 MyApplication 的进程。当警报触发时,MyEventReceiver 开始工作,并且在 DDMS 中我看到两个进程:MyApplication 和 MyApplication:remote。
在实际设备上,如果我启动 MyApplication,添加警报,然后在警报执行时什么也没有发生时使用任务终止程序终止进程。如果我将设备连接到调试器并使用 DDMS 而不是设备上的任务终止程序来停止进程,则警报将触发并按预期工作。
有人可以帮助我理解为什么会发生这种情况吗?我的印象是,一旦安排了闹钟,它就会持续存在,除非设备重新启动。在应执行警报时设备处于唤醒状态。