有没有一种方法的应用程序可以检查它是否被允许访问的通知? [英] Is there a way an app can check if it is allowed to access notifications?
问题描述
我想实现一个NotificationListenerService以便访问投递到通知栏的所有通知。
I want to implement an NotificationListenerService to get access to all notifications that are posted to the notification bar.
据我所知,我需要能够访问该通知在设置与此调用:
I understand that I need to enable access to the Notifications in the Settings with this call:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
有没有一种方法来检查,如果用户启动的通知访问我的应用程序?
Is there a way to check if the user activated Notification access for my app?
推荐答案
我设法找到一个轻微的黑客解决这个问题。如果检查 Settings.Secure
的源代码,你将看到 ENABLED_NOTIFICATION_LISTENERS
标注有 @hide
。我不知道这是否是有意还是无意。在一个相关的话题,CommonsWare不提另一个答案中,有一个的错误 preventing此设置的起点,所以我想是无意的。
I managed to find a slight hack to solve this. If you check the source of Settings.Secure
, you will see that ENABLED_NOTIFICATION_LISTENERS
is annotated with @hide
. I don't know if this is intentional or not. On a related topic, CommonsWare does mention in another answer that there is a bug preventing the starting of this setting so I guess is unintentional.
总之,要解决这个问题,我所做的只是使用的字符串值来获取启用通知监听器的当前目录 ENABLED_NOTIFICATION_LISTENERS
:
Anyway, to work around this, all I did was to fetch the current list of enabled notification listeners using the string value of ENABLED_NOTIFICATION_LISTENERS
:
String enabledListeners = Settings.Secure.getString(context.getContentResolver(),
"enabled_notification_listeners");
然后你只需要检查,如果你的服务是在列表中。
Then you just have to check if your service is in the list.
我不是所有的API> = 18还,但它正在开发的Android 4.4.2进行测试。
I did not test this on all API >= 18 yet but it is working on Android 4.4.2.
这篇关于有没有一种方法的应用程序可以检查它是否被允许访问的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!