fix: catch SecurityException when notification access not granted
This commit is contained in:
@@ -55,9 +55,19 @@ class MediaSessionSource(context: Context) {
|
|||||||
fun start(cb: (MediaInfo?) -> Unit) {
|
fun start(cb: (MediaInfo?) -> Unit) {
|
||||||
stop()
|
stop()
|
||||||
callback = cb
|
callback = cb
|
||||||
|
try {
|
||||||
msm.addOnActiveSessionsChangedListener(activeSessionsListener, listenerComponent)
|
msm.addOnActiveSessionsChangedListener(activeSessionsListener, listenerComponent)
|
||||||
registered = true
|
registered = true
|
||||||
Log.d(TAG, "start: media session listener registered")
|
Log.d(TAG, "start: media session listener registered")
|
||||||
|
} catch (e: SecurityException) {
|
||||||
|
// 未授权「通知使用权」时系统抛 SecurityException:媒体监听降级为不可用,不崩溃。
|
||||||
|
// 用户在系统设置开启通知使用权后,重启 Hearth 才会恢复媒体卡。
|
||||||
|
// MediaSessionService throws SecurityException without notification access;
|
||||||
|
// degrade to media listening disabled instead of crashing. Media card recovers
|
||||||
|
// after the user grants notification access and restarts Hearth.
|
||||||
|
Log.w(TAG, "start: no notification access, media listener disabled: ${e.message}")
|
||||||
|
registered = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注销监听:释放回调引用,供 Activity onDestroy 调用
|
// 注销监听:释放回调引用,供 Activity onDestroy 调用
|
||||||
|
|||||||
Reference in New Issue
Block a user