2014-09-13 102 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()); 

它的工作原理

相關問題