【发布时间】:2014-04-08 17:04:11
【问题描述】:
我想取消我的警报管理器,它启动了一项服务,我使用以下代码创建了它:
Intent intent = new Intent(getActivity(), CheckStatusService.class);
pintent = PendingIntent.getService(getActivity(), 0, intent, 0);
cal.add (Calendar.MINUTE,1);
alarm = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 60*1000, pintent);
我在做
alarm.cancel(pintent)
它没有抛出任何错误,但我开始使用此 Alarmmanager 的服务仍然可以继续工作。
服务完成后,它会向我的一项活动发送广播。在那里它最终被接收,甚至调用了alarm.cancel(),但服务仍然继续运行。
我做错了什么?
【问题讨论】:
-
使用
adb shell dumpsys alarm查看您的闹钟是否仍在安排中。另外,您确定要为cancel()呼叫创建与setRepeating()呼叫相同类型的PendingIntent吗?我问,因为您没有显示为cancel()创建pIntent的位置。 -
@CommonsWare 感谢您的回复!取消警报时我没有重新创建 pIntent,但我使用的是最初创建的 pintent(它是活动中的全局变量)。我知道该服务仍在运行,因为仍然调用 onhandleIntent(使用 Log.e(TAG, message))。
-
"取消警报时我没有重新创建 pIntent,但我使用的是最初创建的 pintent(它是活动中的全局变量)" -- 那么
pIntent可能是null,如果进程在您的应用程序运行之间终止。除此之外,请使用adb shell dumpsys alarm确认 此警报 是导致onHandleIntent()被调用的原因,而不是之前开发中未取消的其他警报.此外,如果您要使用_WAKEUP警报,请使用WakefulBroadcastReceiver或我的WakefulIntentService,而不仅仅是简单的IntentService。
标签: android android-intent service android-pendingintent