微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – NotificationListenerService:getActiveNotifications上的NullPointerException

根据本教程,我试图在我的应用程序中实现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

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

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

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

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


    }
}

解决方法

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

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

到目前为止,我有完全相同的问题.事实证明,凌驾于对方是危险的.如果您重写onBind,则必须返回super.onBind(intent)返回的相同IBinder.如果要返回自己的自定义绑定器,请确保使用唯一的意图,并且仅在收到自定义意图时返回自定义绑定.

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

一旦您授予读取通知的权限,系统就会在您的服务上调用onBind.如果您的onBind向系统返回自定义绑定,系统将不会给您通知,并且可能会导致Null指针或安全异常.

希望这有帮助!

原文地址:https://www.jb51.cc/java/123391.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐