NotificationManagerCompat花太长时间来更新已启用的侦听器程序包

时间:2019-01-23 11:53:41

标签: android notifications

问题:还有另一种检查通知访问的方法比使用NotificationManagerCompat.getEnabledListenerPackages()更准确?

我的代码正在发生的情况的说明:

我的应用程序中有一项读取通知的服务,因此它会在其初始化方法中验证是否具有读取通知的权限。

问题是验证此权限的代码在我授予访问权限后立即返回false(在init()上调用onListenerConnected()是一种{{1}的方法},并且NotificationListenerService内有一个init()弄乱了我的应用)。

hasPermission()非常简单,但是为了进行调查,我做了一些改编:

hasPermission()

然后我意识到class PermissionsManager { // ... public static boolean hasPermission() { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) { return false; } boolean permission = false; for (int i = 0; i < 100; i++) { permission = NotificationManagerCompat .getEnabledListenerPackages(context) .contains(context.getPackageName()); Log.d(TAG, "permission to access notifications: " + (permission ? "granted" : "denied")); if (permission) break; } return permission; } } 的更新速度不够快:

NotificationManagerCompat.getEnabledListenerPakcages()

以前,我使用以下代码获取许可,但结果相同:

01-23 09:45:39.008 I/chatty: uid=10251 com.example.app:notifications identical 5 lines
01-23 09:45:39.008 D/PermissionsManager: permission to access notifications: denied
01-23 09:45:39.009 D/PermissionsManager: permission to access notifications: denied
01-23 09:45:39.009 I/chatty: uid=10251 com.example.app:notifications identical 5 lines
01-23 09:45:39.010 D/PermissionsManager: permission to access notifications: denied
01-23 09:45:39.011 D/PermissionsManager: permission to access notifications: granted

0 个答案:

没有答案