如何取消Android的通知? [英] How to cancel Android notifications?

查看:140
本文介绍了如何取消Android的通知?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出一个应用程序,其中用户可以设置基于GPS位置告警。我只想要1警报是活跃在任何一个时间。所以,当用户设置了第二报警,我想取消通知的第一报警(然后设置一个新的通知的第二报警)。

I am making an app where the user can set an alarm based on GPS location. I only want 1 alarm to be active at any one time. So, when the user sets a 2nd alarm, I want to cancel the notification for the 1st alarm (then set a new notification for the 2nd alarm).

现在,我的通知继续堆叠(如我不能删除它们,所以它们都有效)。这是我的code,其中我试图删除警报和通知(S):

Right now, my notifications continue to stack up (as in I can't delete them, so they are all active). Here is my code where I am trying to delete the alarm and notification(s):

// 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()函数(我真的不知道什么时候这就是所谓的...)

This is the onDestroy() function in my AlarmService.class (I'm not really sure when this is called...)

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();
}

那么,这是我如何设置一个新的报警和通知:

Then, this is how I am setting a new alarm and notification:

Intent intentAlarmService2 = new Intent(v.getContext(), AlarmService.class);
startService(intentAlarmService2);

顺便说一句,我的AlarmService.class是工作是肯定的。

By the way, my AlarmService.class is working for sure.

在此先感谢。

推荐答案

首先,摆脱 getApplicationContext的()。你几乎永远不需要它,它常常是错误的选择。与,因为无论你是叫 getApplicationContext()上的的一个<替换它code>上下文。

First, get rid of getApplicationContext(). You almost never need it and it is frequently the wrong choice. Replace it with this, since whatever you are calling getApplicationContext() on is a Context.

在code您列出,你从来没有提出一个通知。因此,也很难帮助你弄清楚为什么你得到不止一个。呼叫 cancelAll() NotificationManager 应该从你的应用程序摆脱所有悬而未决的通知。

In the code you have listed, you never raise a Notification. Hence, it is difficult to help you figure out why you are getting more than one. Calling cancelAll() on the NotificationManager should get rid of all outstanding notifications from your application.

我最好的猜测是,的onDestroy()不会被调用你的服务。如果别的东西是保持在内存中的服务,将发生(例如,你必须把它通过活跃绑定连接bindService())。或者,可能的话,你有什么奇怪的在你的&LT;服务&GT; 在清单(元素例如,一个不必要的安卓过程属性)被污染了 NotificationManager 取消()操作。

My best guess is that onDestroy() is not being called on your service. That would occur if something else is keeping the service in memory (e.g., you have an active bound connection to it via bindService()). Or, possibly, you have something strange in your <service> element in the manifest (e.g., an unnecessary android:process attribute) that is fouling up the NotificationManager cancel() operation.

这篇关于如何取消Android的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆