本文介绍了使用 NotificationListenerService 检查对通知的访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 >=4.3 NotificationListenerService 来访问通知.在第一次启动时,我的应用程序会将用户带到访问通知"系统面板,但是只要我的应用程序在访问通知"中的复选框被禁用,我就会将用户带到那里.我还没有在任何地方找到 isNotificationAccessEnabled() 方法,但我绝对知道这是可能的,因为像 Krome 也这样做.

I'm using the >=4.3 NotificationListenerService to access notifications. On the first start, my app takes the user to the "Access Notifications" system panel, but I'd like to take the user there whenever the checkbox for my app in "Access Notifications" is disabled. I haven't found a isNotificationAccessEnabled()-method anywhere, but I definitely know that it's possible because apps like Krome do this, too.

推荐答案

从 Android 8.1 (SDK 27) 开始,您可以调用 isNotificationListenerAccessGranted.这是要使用的正确 API.较旧的 Android 版本应使用 getEnabledListenerPackages 作为次优选择.依赖您的侦听器回调可能会产生不正确的结果.请参阅下面的说明.

Starting with Android 8.1 (SDK 27) you can call isNotificationListenerAccessGranted on the NotificationManager. This is the correct API to use. Older Android versions should use getEnabledListenerPackages as a second best option. Relying on your listener callbacks can give incorrect results. See explanation below.

我是 Krome 的开发者.我为检查服务是否启用所做的工作是添加公共静态变量,该变量在 onBind 方法中更改为 true,在 unbind 中更改为 false.这就是这项服务的运作方式.

Im developer of Krome. What have I done to check if service is enabled is add public static variable that changes to true in onBind method and to false in unbind. That is how this service work.

public static boolean isNotificationAccessEnabled = false;

@Override
public void onListenerConnected() {
    isNotificationAccessEnabled = true;
}

@Override
public void onListenerDisconnected() {
    isNotificationAccessEnabled = false;
}

这篇关于使用 NotificationListenerService 检查对通知的访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:08