【问题标题】:NotificationListenerService requires to toggle notification access on every launch to workNotificationListenerService 需要在每次启动时切换通知访问才能工作
【发布时间】:2020-06-02 18:09:47
【问题描述】:

Manifest.xml

    <service android:name=".CustomNotificationListene"
        android:label="@string/app_name"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
    </service>

自定义通知监听类

class CustomNotificationListene : NotificationListenerService() {
private var muted = false
private var originalVolume = 0
private var zeroVolume = 0
private var blocklist = listOf<String>("Advertisement","spotify")


override fun onUnbind(intent: Intent?): Boolean {
    return super.onUnbind(intent)
}


override fun onStartCommand(intent: Intent, flags: Int, startID: Int): Int {
    timer = Timer()
    isRunning = true
    muted = false
    originalVolume = 0
    zeroVolume = 0

    //Some function



override fun onDestroy() {
    try {
        killService()
    } catch (ex: NullPointerException) {
    }
}

override fun onNotificationPosted(notification: StatusBarNotification) {}
override fun onNotificationRemoved(notification: StatusBarNotification) {}


}

在 oncreate 方法中调用 customnotificationlistene 类的 Mainactivity

  var serviceIntent = Intent(this, CustomNotificationListene::class.java)
  startService(serviceIntent)

Logcat信息

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

(每秒 4 次直到结束)

问题:每次启动应用程序时,我都必须手动切换通知访问权限,以使通知侦听器工作。

我的问题: 我需要阅读另一个应用程序的通知而不是垃圾邮件

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

每次用户打开我的应用时。
其他需要通知访问权限的应用程序,例如 mi fit 或 truecaller,只需要切换一次通知访问权限,并且可以完美运行,直到其关闭。所以一定有办法。

是否有任何方法可以保存给定的访问权限,例如使用 getSharedPreferences 来检测应用的首次启动?

尝试过的修复:
几乎所有关于 NotificationListenerService 的 stackoverflow 解决方案,但没有运气。

回答我的要求:每当我的应用程序的通知访问被关闭并且没有得到时,需要提示用户

2020-06-02 23:22:24.401 3375-3671/com.example.verse W/NotificationListenerService[CustomNotificationListene]: Notification listener service not yet bound.

请帮帮我!!我已经在这个问题上花了大约 3 天时间。

【问题讨论】:

    标签: android kotlin notifications listener android-permissions


    【解决方案1】:

    我不知道它是否正确,但在做了一些研究后,我发现当设备连接到电脑进行调试时,NotificationListenerService 显示了一些异常。 我暂时通过提示用户打开和关闭对我的应用程序的通知访问来修复它,只要它不工作,然后它就可以正常工作。

    如果有任何 100% 工作概率的有效修复,请告诉我。

    【讨论】:

      猜你喜欢
      • 2017-07-17
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多