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

Android NotificationListenerService抛出DeadObjectException

我有一个简单的NotificationListenerService实现来测试新的4.3 API.服务本身曾经工作过.之后,我添加了在添加特定包的通知时发送广播.现在,只要我启动该服务,它就会抛出一个DeadobjectException.这是堆栈跟踪:
E/NotificationService﹕ unable to notify listener (posted): android.service.notification.INotificationListener$Stub$Proxy@42c047a0
    android.os.DeadobjectException
    at android.os.BinderProxy.transact(Native Method)
    at android.service.notification.INotificationListener$Stub$Proxy.onNotificationPosted(INotificationListener.java:102)
    at com.android.server.notificationmanagerService$NotificationListenerInfo.notifyPostedIfUserMatch(notificationmanagerService.java:241)
    at com.android.server.notificationmanagerService$2.run(notificationmanagerService.java:814)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at com.android.server.ServerThread.run(SystemServer.java:1000)

这就是我启动服务的方式

@Override
public boolean onoptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_start_service:
            startService(new Intent(this,ConnectService.class));
            return true;
        default:
            return super.onoptionsItemSelected(item);
    }
}

我可以验证服务是否启动,因为我登录了onCreate()和onDestroy().
以下是如何处理通知发布的方式:

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    Log.i(TAG,sbn.getNotification().toString());
    if (sbn != null && sbn.getPackageName().equalsIgnoreCase(PKG)) {
        Intent intent = new Intent(ConnectService.NOTIFY);
        intent.putExtra("notification",sbn.getNotification().toString());
        bManager.sendbroadcast(intent);
    }
}

糟糕的是堆栈跟踪是没用的.出了什么问题?

解决方法

尽量不要自己启动服务.如果已在安全设置中启用NotificationListenerService,则系统应自动绑定到它.

或者,检查崩溃日志以查看服务是否崩溃或其进程是否已被终止.我相信有一个错误,如果您的NotificaitonListerService死亡,系统将不会重新绑定,直到您重新启动手机或在安全设置中切换通知权限.

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

相关推荐