2014-09-13 44 views
0

我试图取消/删除从状态栏的通知,通过cancelNotification()方法从我NotificationListenerService内,使用下面的代码:NotificationListenerService.cancelNotification引发NullPointerException异常

public final void mcancelNotification(String pkgn, String t, int i){ 
    cancelNotification(pkgn, t, i); //line 84 
} 

但是我得到一个NullPointerException (见下文),并做了一些测试后,我注意到它是tag tnull。 这是我得到了来自onNotificationPosted方法中的pkgnti值:

packageName = sbn.getPackageName(); 
    tag = sbn.getTag(); 
    id = sbn.getId(); 

如何删除通知,如果该标签等于空?我错过了什么吗?真的很感激一些帮助,感谢

NPE:

09-13 14:23:30.270: E/AndroidRuntime(29456): FATAL EXCEPTION: main 
09-13 14:23:30.270: E/AndroidRuntime(29456): Process: com.project.now, PID: 29456 
09-13 14:23:30.270: E/AndroidRuntime(29456): java.lang.NullPointerException 
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.os.Parcel.readException(Parcel.java:1471) 
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.os.Parcel.readException(Parcel.java:1419) 
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.app.INotificationManager$Stub$Proxy.cancelNotificationFromListener(INotificationManager.java:469) 
09-13 14:23:30.270: E/AndroidRuntime(29456): at android.service.notification.NotificationListenerService.cancelNotification(NotificationListenerService.java:116) 
09-13 14:23:30.270: E/AndroidRuntime(29456): at com.project.now.NoLiSes.mcancelNotification(NoLiSes.java:84) 
+0

com.project.now.NoLiSes的placecode。mcancelNotification.NoLiSes.java line 84 – 2014-09-13 12:28:26

+0

请发布一些更多的代码......当你打电话给mcancelNotification(String pkgn,String t,int i) – sunil 2014-09-13 12:31:54

+0

@Imtiyaz line 84发布后,我编辑了我的帖子并在上面的代码中标记了它 – REG1 2014-09-13 12:32:21

回答

1
public static void cancelNotification(Context ctx, int notifyId) { 
    String ns = Context.NOTIFICATION_SERVICE; 
    NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns); 
    nMgr.cancel(notifyId); 
} 

//取消先前显示通知。

cancel(String tag, int id) 

//通知通知管理员关于解除单个通知。

cancelNotification(String pkg, String tag, int id) 

链接http://developer.android.com/reference/android/app/NotificationManager.html#cancel%28java.lang.String,%20int%29

链接https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

public static final int FLAG_AUTO_CANCEL 

在API级别1

位被按位或到如果通知应该是应设置标志字段被用户点击时取消。

//清除所有通知

NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
nMgr.cancelAll(); 

,但你无法取消其他应用的通知,那是不可能的。

+0

我试着你在顶部发布的四行,但我得到了关于ctx的NullPointerException ...我用getApplicationContext()而不是通过它传递一个ctx参数。为什么这不起作用?感谢您的帮助 – REG1 2014-09-13 12:53:29

+0

请发布您的电话号码mcancelNotification – sunil 2014-09-13 13:01:26

+0

我将其更改为您发布的准确代码的类别,现在什么都没有发生:没有例外,也没有删除通知。如果我理解正确,那么您发布的代码用于我自己发布的通知。尽管我想要做的是能够删除任何通知,无论我是否是发布通知的人。使用需要用户许可的NLS。我很欣赏帮助,谢谢...纠正我,如果我错了 – REG1 2014-09-13 13:06:49

0

我找到了解决方案!

首先,您必须只有一个NotificationListenerService实例。在我的应用程序中,我将以单数形式处理我的通知,并从许多课程与他们一起工作。这是我服务的一部分:

private ServiceListener listener; 
    private NotificationsHolder holder; 

    @Override 
    public void onCreate() { 
    super.onCreate(); 
    packageManager = getPackageManager(); 
    holder = NotificationsHolder.getInstance(); 
    listener = holder; 
    listener.registerService(this); 
    } 

的的ServiceListener接口:

public interface ServiceListener { 
    void registerService(NotificationService service); 
    void unregisterService(); 
    } 

在NotificationsHolder类(它是一个singletone):

@Override 
    public void registerService(NotificationService service) { 
    this.service = service; 
    } 

    @Override 
    public void unregisterService() { 
    service = null; 
    } 

    public NotificationService getService(){ 
    return service; 
    } 

而现在,只要我需要取消通知,我这样做:

holder = NotificationsHolder.getInstance(); 
    NotificationService service = holder.getService(); 
    service.cancelNotification (myNotification.getPackageName(), myNotification.getTag(), myNotification.getIndex()); 

它的工作原理