AlarmManager 无法取消警报
AlarmManager can't cancel an alarm
看看有没有人发现这个问题,帮我解开谜团。我有以下情况。该应用程序是一个简单的待办事项列表:
public class NewToDo extends Activity {
private PendingIntent pendingIntent;
private AlarmManager manager;
...
当用户创建带有通知的待办事项时,我使用此 class 的方法之一创建它,如下所示:
int interval = 30000;
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
/*Let's let the putExtras apart, as documentation says they're not considered when comparing intents, but I add some of them*/
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, alarmIntent, alarmIntent.FILL_IN_DATA);
/*Printing _id we see 3, for instance, but is a unique identifier to associate the notification with this to-do item*/
/*I use the flag alarmIntent.FILL_IN_DATA because I want to send extras to create a custom notification*/
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval, interval,
pendingIntent);
我退出应用程序(但如果我不退出也没有区别)。然后通知每 30 秒正确到达一次,选择它应用程序将在项目的详细视图上启动。操作栏按钮允许对其进行编辑,再次调用相同的 class,NewToDo。
在编辑时我可以取消与待办事项相关的通知,我的代码是这样的 - 在 NewToDo class-:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class); //creating a matching Intent
Log.v(getString(R.string.app_name), "_id: " + _id); //the 3 aforementioned
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), _id, i, alarmIntent.FILL_IN_DATA);
alarmManager.cancel(displayIntent);
但是通知会每 30 秒出现一次,因此不会被取消。
我在另一个 post 中看到了以下建议:
"Intent intentYouWantToCancel = new Intent(this, AlarmReceive.class);
intentYouWantToCancel.setAction("aRelevantString");
when you set the alarmmanager as well as when you want to delete it. "
所以我将两个意图作为 'aRelevantString' 转换为唯一标识符的字符串。但同样的问题。
还更改了 this 的 getBaseContext(),以防万一,但没有区别。
没有错误,只是通知不断重复,忽略了我的取消。任何想法?
PS:每次 运行 我都会重新启动 AVD。我意识到我使用 filterEquals 打印的方式根本没有任何意义(为此花费了太多时间),但这并不影响问题的其余部分。
如下所示修改您的代码并尝试,这可能对您有所帮助。这是我之前从 SO 本身使用的那个:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
终于这样解决了。要创建警报:
public class NewToDo extends Activity {
private PendingIntent pendingIntent;
private AlarmManager manager;
...
int interval = 30000;
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("id", _id);
pendingIntent = PendingIntent.getBroadcast(this, _id, alarmIntent,0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval, interval,
pendingIntent);
要取消那个具体的警报:
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), _id, i, 0);
manager.cancel(pendingIntent);
pendingIntent.cancel();
看看有没有人发现这个问题,帮我解开谜团。我有以下情况。该应用程序是一个简单的待办事项列表:
public class NewToDo extends Activity {
private PendingIntent pendingIntent;
private AlarmManager manager;
...
当用户创建带有通知的待办事项时,我使用此 class 的方法之一创建它,如下所示:
int interval = 30000;
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
/*Let's let the putExtras apart, as documentation says they're not considered when comparing intents, but I add some of them*/
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), _id, alarmIntent, alarmIntent.FILL_IN_DATA);
/*Printing _id we see 3, for instance, but is a unique identifier to associate the notification with this to-do item*/
/*I use the flag alarmIntent.FILL_IN_DATA because I want to send extras to create a custom notification*/
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval, interval,
pendingIntent);
我退出应用程序(但如果我不退出也没有区别)。然后通知每 30 秒正确到达一次,选择它应用程序将在项目的详细视图上启动。操作栏按钮允许对其进行编辑,再次调用相同的 class,NewToDo。 在编辑时我可以取消与待办事项相关的通知,我的代码是这样的 - 在 NewToDo class-:
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, AlarmReceiver.class); //creating a matching Intent
Log.v(getString(R.string.app_name), "_id: " + _id); //the 3 aforementioned
PendingIntent displayIntent = PendingIntent.getActivity(getBaseContext(), _id, i, alarmIntent.FILL_IN_DATA);
alarmManager.cancel(displayIntent);
但是通知会每 30 秒出现一次,因此不会被取消。
我在另一个 post 中看到了以下建议:
"Intent intentYouWantToCancel = new Intent(this, AlarmReceive.class); intentYouWantToCancel.setAction("aRelevantString");
when you set the alarmmanager as well as when you want to delete it. "
所以我将两个意图作为 'aRelevantString' 转换为唯一标识符的字符串。但同样的问题。
还更改了 this 的 getBaseContext(),以防万一,但没有区别。
没有错误,只是通知不断重复,忽略了我的取消。任何想法?
PS:每次 运行 我都会重新启动 AVD。我意识到我使用 filterEquals 打印的方式根本没有任何意义(为此花费了太多时间),但这并不影响问题的其余部分。
如下所示修改您的代码并尝试,这可能对您有所帮助。这是我之前从 SO 本身使用的那个:
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(sender);
终于这样解决了。要创建警报:
public class NewToDo extends Activity {
private PendingIntent pendingIntent;
private AlarmManager manager;
...
int interval = 30000;
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(this, AlarmReceiver.class);
alarmIntent.putExtra("id", _id);
pendingIntent = PendingIntent.getBroadcast(this, _id, alarmIntent,0);
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis() + interval, interval,
pendingIntent);
要取消那个具体的警报:
manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), _id, i, 0);
manager.cancel(pendingIntent);
pendingIntent.cancel();