fix: catch SecurityException when notification access not granted

This commit is contained in:
2026-08-16 18:10:26 +08:00
parent 160274e218
commit 8e722dc545
@@ -55,9 +55,19 @@ class MediaSessionSource(context: Context) {
fun start(cb: (MediaInfo?) -> Unit) {
stop()
callback = cb
try {
msm.addOnActiveSessionsChangedListener(activeSessionsListener, listenerComponent)
registered = true
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 调用