android - 通知监听服务 : detect if application is allowed to listen for notification

标签 android android-notifications

我看到一些应用程序有一个小对话框,要求允许应用程序监听通知。该对话框有 2 个按钮:取消和执行(打开安全设置以允许应用程序监听通知)。该对话框是持久的,所以我猜它有一种方法来检测该应用程序是否被允许。任何人都可以向我指出该 API 吗?谢谢

最佳答案

我知道这是一个老问题,但这是我现在在我的应用程序中使用的:

String notificationListenerString = Settings.Secure.getString(this.getContentResolver(),"enabled_notification_listeners");
//Check notifications access permission
if (notificationListenerString == null || !notificationListenerString.contains(getPackageName()))
{
      //The notification access has not acquired yet!
}else{
      //Your application has access to the notifications
}

您可以通过打开 Activity 将用户移至通知访问权限设置:

startActivity(new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS")); 

从 Jelly Bean 4.3 到 Marshmallow 6.0 都经过了很好的测试,我在我的应用程序中使用了它。

关于android - 通知监听服务 : detect if application is allowed to listen for notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22289883/

相关文章:

android - React Native如何获取App启动源?

android - MotionLayout中的TextView在大小动画上闪烁

android - 启动器 Activity 已更改,这就是 FCM 推送通知在应用程序处于后台时不起作用的原因

android - 未收到通知的 PendingIntent

android - 在没有通知 channel 的情况下使用 Firebase 推送通知

java - 如何获取从互联网上获取的 Drawable 并将其保存到我的 Android 设备的缓存目录中?

android - if else语句在android中

android.content.res.Resources$NotFoundException : File res/mipmap-xhdpi-v4/ic_copyright_black_24dp. png 来自 xml 类型 xml 资源 ID #0x7f0d0007

Android 提醒和任务应用教程

android - 自 Oreo/API 26 起允许用户设置通知灯颜色