Android NotificationListenerService 抛出 DeadObjectException

标签 android android-service

我有一个简单的 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,系统应该会自动绑定(bind)到它。

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

关于Android NotificationListenerService 抛出 DeadObjectException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18802076/

相关文章:

android - Retrofit-2.0 - 解析在 xml 中包含 json 的响应

android - 停止远程进程中的服务

java - Android 传感器在单独的线程中注册监听器

android - 当 wifi 或 3g 网络状态改变时的 BroadcastReceiver

android - 如何仅在退出应用程序时结束服务?

android - 如何创建三角形自定义形状android

android - 需要在 android 中使用相机捕获矩形 View 内的图像

java - android.view.InflateException 由 : java. lang.OutOfMemoryError 引起

android - 如何将参数从多个 Activity 传递到一个 Activity

android - Parcelable 接口(interface)中的 readFromParcel() 方法