NotificationListenerService要求在每次启动时切换通知访问才能正常工作

问题描述 投票:0回答:1

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>

customnotificationlistene类

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.

请帮帮我!!我已经在这单期中度过了三天。

Manifest.xml ...

android kotlin notifications listener android-permissions
1个回答
0
投票

我不知道它的答案是否正确,但是在进行了一些研究之后,我发现当设备连接到PC进行调试时,NotificationListenerService会显示一些异常。我暂时通过提示用户在我的应用程序不起作用时打开和关闭对我的应用程序的通知访问权限来修复它,然后它正常工作。

请让我知道是否有有效的修复方法,其工作可能性为100%。

© www.soinside.com 2019 - 2024. All rights reserved.