android - NotificationListenerService 停止并且不重启就无法重启

标签 android android-notifications android-framework

在 android 4.4 中,我发现一些用户使用我实现的 notificationListenerService 将停止工作。我自己也亲眼见过。

我从来没有设法在它发生时捕捉到任何 logcat,但即使进入通知访问部分并关闭并重新打开通知访问也无济于事。

有没有什么东西可以导致这种情况发生。我不确定我的应用程序是否崩溃并导致此问题,但即使如此,重启监听器的唯一方法似乎是重启手机。还有其他方法可以让我的应用程序从监听器服务接收通知吗?

我还收到用户在更新应用程序版本时遇到此问题的报告。

我已经尝试将日志放入 onCreate/onDestroy 方法中,以查看它停止时是否记录了任何内容,但是调用了 onUnbind、onDestroy,但是这些方法中的任何一个都没有输入任何内容,所以我猜它们没有被调用一旦停止。

编辑: 发生这种情况后,我捕获了一份错误报告,日志中有趣的部分看起来是:

12-30 15:33:58.731 16194 16194 W ContextImpl: Calling a method in the system process without a qualified user: android.app.ContextImpl.sendOrderedBroadcast:1192 android.app.ContextImpl.sendOrderedBroadcast:1183 android.content.ContextWrapper.sendOrderedBroadcast:390 com.android.settings.applications.ProcessStatsDetail.checkForceStop:314 com.android.settings.applications.ProcessStatsDetail.onResume:108 
12-30 15:33:58.741   780   780 V NotificationService: disabling notification listener   for user 0: ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.NotificationListener}
12-30 15:33:58.741   780   780 V NotificationService: disabling notification listener for user 0: ComponentInfo{lovetere.flashme/lovetere.flashme.accessibilityservice.ServicePostKitKat}
12-30 15:33:58.741   780   780 V NotificationService: enabling notification listener for user 0: ComponentInfo{com.example.android.navigationdrawerexample/com.example.android.navigationdrawerexample.NotificationListener}
12-30 15:33:58.741   780   780 V NotificationService: enabling notification listener for user 0: ComponentInfo{lovetere.flashme/lovetere.flashme.accessibilityservice.ServicePostKitKat}
12-30 15:33:58.751  1171  1171 D MyApp:NotificationListener: [NotificationListener] onDestroy

所以看起来有一个警告,然后将停止服务并重新启动它(它发生在我的应用程序和我安装的另一个应用程序中,它使用通知访问查看两个正在停止的应用程序)

真的不知道我能做些什么来阻止这种停止

最佳答案

这种方式可以重启,但需要10s左右!

private void toggleNotificationListenerService() {
        PackageManager pm = getPackageManager();
        pm.setComponentEnabledSetting(new ComponentName(this, com.xinghui.notificationlistenerservicedemo.NotificationListenerServiceImpl.class),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

        pm.setComponentEnabledSetting(new ComponentName(this, com.xinghui.notificationlistenerservicedemo.NotificationListenerServiceImpl.class),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    }

作者:雨果

链接: https://www.zhihu.com/question/33540416/answer/113706620

关于android - NotificationListenerService 停止并且不重启就无法重启,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20198283/

相关文章:

android - Google+ 使用 Play 服务登录 6.5.87 (GoogleApiClient) - Android

android - 铃声音量静音时使用警报流的声音通知 (Android Oreo)

android - 如何像 Google Play 音乐一样将我的前台服务通知 (MediaStyle) 保持在最前面?

android - 清除编辑文本 - adb

android - 如何解决java.lang.NoSuchMethodError : android. widget.AbsListView.isItemChecked

android - Dalvik 虚拟机占用的大小

android - 如何获取当前电池百分比?

android - 在 Android 中使用 GridView 是否安全?

系统应用程序的 Android java.lang.SecurityException

android - 自定义通知布局和文本颜色