【发布时间】:2015-05-06 19:45:52
【问题描述】:
我正在制作一个应用程序,用户可以在其中根据 GPS 位置设置警报。我只想在任何时候激活 1 个警报。所以,当用户设置第二个闹钟时,我想取消第一个闹钟的通知(然后为第二个闹钟设置一个新的通知)。
现在,我的通知继续堆积(因为我无法删除它们,所以它们都处于活动状态)。这是我试图删除警报和通知的代码:
// Stop the location alarm activity
Intent intentAlarmService_delete = new Intent(v.getContext(), AlarmService.class);
stopService(intentAlarmService_delete); // I think this calls onDestroy() in AlarmService class ...
mNtf = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNtf.cancelAll();
Intent alarmIntent2 = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
这是我的 AlarmService.class 中的 onDestroy() 函数(我不确定何时调用...)
public void onDestroy(){
super.onDestroy();
mNtf.cancel(NOTIFICATION_ID1);
Intent alarmIntent = new Intent(getApplicationContext(), OneTimeAlarmReceiver.class);
PendingIntent pendingIntentAlarm = PendingIntent.getBroadcast(getApplicationContext(), PENDING_INTENT_REQUEST_CODE1,
alarmIntent, PendingIntent.FLAG_CANCEL_CURRENT);
pendingIntentAlarm.cancel();
Intent intentAlarmService = new Intent(getApplicationContext(), AlarmService.class);
stopService(intentAlarmService);
mNtf.cancel(NOTIFICATION_ID1);
mNtf.cancelAll();
}
然后,这就是我设置新警报和通知的方式:
Intent intentAlarmService2 = new Intent(v.getContext(), AlarmService.class);
startService(intentAlarmService2);
顺便说一句,我的 AlarmService.class 肯定可以工作。
提前致谢。
【问题讨论】:
标签: android