【发布时间】: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