我希望实现一个NotificationListenerService来获取发布到通知栏的所有通知。
我了解需要使用以下调用在设置中启用对通知的访问:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
有没有办法检查用户是否为我的应用程序激活了通知访问权限?
我希望实现一个NotificationListenerService来获取发布到通知栏的所有通知。
我了解需要使用以下调用在设置中启用对通知的访问:
startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS"));
有没有办法检查用户是否为我的应用程序激活了通知访问权限?
Settings.Secure
的源代码,您会发现 ENABLED_NOTIFICATION_LISTENERS
被注释为 @hide
。我不知道这是否是有意为之。在相关话题上,CommonsWare 在另一个答案中提到,有一个bug阻止了此设置的启动,所以我想这可能是无意的。
无论如何,为了解决这个问题,我所做的就是使用ENABLED_NOTIFICATION_LISTENERS
的字符串值获取当前已启用通知监听器的列表:
String enabledListeners = Settings.Secure.getString(context.getContentResolver(),
"enabled_notification_listeners");
那么你只需要检查一下你的服务是否在列表中。
我还没有在所有API >= 18上测试过这个,但是它可以在Android 4.4.2上工作。
可能适用于API 19(4.4)及更高版本。 虽然我在API 21(5.0)上尝试过。
String enabledListeners = Settings.Secure.getString(context.getContentResolver(),
"enabled_notification_listeners");
secured"正在变成红色。如果您想使用它,您必须使用:
"String enabledNotificationListeners =
android.provider.Settings.Secure.getString(context.getContentResolver(),
"enabled_notification_listeners");
尝试:
NotificationManagerCompat.from(context).areNotificationsEnabled()
public class NotifyListener extends NotificationListenerService{
public static boolean listnerConnected = false;
@Override
public IBinder onBind(Intent intent) {
Log.d(name,"onBind Called");
listnerConnected = true;
return super.onBind(intent);
}
@Override
public void onDestroy()
{
super.onDestroy();
Log.e("destroy", "called");
listnerConnected = false;
}
}