NotificationListenerService:getActiveNotifications上的NullPointerException

Ver*_*lst 10 java notifications android

我正在尝试根据本教程在我的应用程序中实现NotificationListenerService:http://www.kpbird.com/2013/07/android-notificationlistenerservice.html,但是在调用getActiveNotifications时我遇到了NullPointerException.

Caused by: java.lang.NullPointerException
at android.os.Parcel.readException(Parcel.java:1437)
at android.os.Parcel.readException(Parcel.java:1385)


at android.app.INotificationManager$Stub$Proxy.getActiveNotificationsFromListener(INotificationManager.java:500)
at android.service.notification.NotificationListenerService.getActiveNotifications(NotificationListenerService.java:149)
at com.rootsoft.rsnotificationservice.RSNotificationService.activeNot(RSNotificationService.java:85)
at com.rootsoft.rsnotificationservice.RSNotificationService.access$0(RSNotificationService.java:81)
at com.rootsoft.rsnotificationservice.RSNotificationService$1.onReceive(RSNotificationService.java:105)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:763)
... 9 more
Run Code Online (Sandbox Code Playgroud)

我正在向服务发送广播,该广播应生成所有通知的列表:

private void activeNot () {
    List l = new List();
    l.Initialize();

    for (StatusBarNotification sbn : getActiveNotifications() ) { <---- Error happens here
        l.Add(sbn);
    }

    Log.i("B4A", "List created.");


    }
}
Run Code Online (Sandbox Code Playgroud)

dem*_*319 16

编辑:我已经了解了更多关于这一点,并让它工作!

注意:首先,请确保您已在Android设备的"通知访问权限"设置窗格中启用了应用.

直到现在我才遇到完全相同的问题.事实证明,压倒onBind是危险的.如果你覆盖onBind,你必须返回返回的同一个IBinder super.onBind(intent).如果要返回自己的自定义绑定器,请确保使用唯一意图,并仅在收到自定义意图时返回自定义绑定器.

@Override
public IBinder onBind(Intent intent)
{
    if (intent.getAction().equals("custom_intent"))
    {
        return customBinder;
    }
    else
    {
        return super.onBind(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

一旦您授予其阅读通知的权限,系统就会对您的服务调用onBind.如果你的onBind向系统返回一个自定义绑定器,系统将不会给你通知,并可能导致空指针或安全例外.

希望这有帮助!

  • 好的解决方案 另外一点...... Android在启动时或在首次检查"启用通知"时绑定到您的服务.在实施onBind解决方案后,我需要取消选中/重新检查启用通知按钮,以使绑定正常工作. (2认同)