【问题标题】:How to cancel Android notifications?如何取消安卓通知?
【发布时间】: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


    【解决方案1】:

    首先,摆脱getApplicationContext()。您几乎从不需要它,而且它经常是错误的选择。将其替换为this,因为您在上调用getApplicationContext() 的任何内容都是 Context

    在您列出的代码中,您永远不会提出Notification。因此,很难帮助您弄清楚为什么您会得到不止一个。在 NotificationManager 上调用 cancelAll() 应该会删除应用程序中所有未完成的通知。

    我的最佳猜测是您的服务没有调用onDestroy()。如果有其他东西将服务保留在内存中(例如,您通过bindService() 与它建立了活动绑定连接),就会发生这种情况。或者,您可能在清单中的 <service> 元素中有一些奇怪的东西(例如,不必要的 android:process 属性),这会破坏 NotificationManager cancel() 操作。

    【讨论】:

    • 嗯,我检查了你建议的所有东西,但它们都不起作用......(我不能使用“this”,因为我似乎遇到了错误 - 这可能是所有代码的原因在“public void onClick(View v)”函数中。)+(我没有使用 onBind();)+(在 中没有奇怪的东西)
    • 我将准确描述正在发生的事情:我尝试在 NotificationManager 上调用 cancelAll(),但是:1) 我设置了一个新通知(它显示在“通知窗口”中 - 当你从屏幕顶部滑动手指到底部)2)我删除了通知(它从“通知窗口”中消失了3)我设置了一个新的通知***(它显示在“通知窗口”中,但然后是上一个通知(已删除)显示在最新通知(刚刚设置)之上,并且 2 个通知在“通知窗口”中不断闪烁/交替
    • @Yasir Malang:“我不能使用“this”,因为我似乎遇到了错误”——那么,你在一个内部类中,需要使用 MyOuterClass.this(替换你的MyOuterClass 的活动类名称)。
    • cancelAll() 是我想要的。
    • 是我的错:我在调用 unregisterReceiver() 之后调用了 cancelAll 和 can(),只是在服务中的函数 unregisterReceiver() 之前调用:))
    【解决方案2】:

    您需要确保始终引用同一 NotificationManager 实例。不同的实例会产生不同的通知。我建议使用服务来管理通知。

    http://developer.android.com/guide/topics/fundamentals.html

    【讨论】:

    • NotificationManager 只是在另一个进程中运行的系统服务的代理。我有一个示例项目 (github.com/commonsguy/cw-android/tree/master/Notifications/…),它不像您描述的那样工作。如果您有证据证明 NotificationManager 可以按照您的描述工作,请指点我!
    • 使用不同的 NotificationManager 实例不会产生不同的通知。这完全取决于您的通知 ID。
    猜你喜欢
    • 2011-12-15
    • 1970-01-01
    • 2019-05-04
    • 2011-05-25
    • 2011-07-20
    • 2017-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多