前台启动失败自动重试;属性图标视觉高度统一;统计标题简化(v0.11)
This commit is contained in:
@@ -101,6 +101,9 @@ class TimerService : Service() {
|
||||
if (TimerCore.running && !TimerCore.countUp) startTicker() else stopTicker()
|
||||
}
|
||||
|
||||
/** EMUI 偶发拒绝前台启动时的自愈重试标记:只重试一次,避免无限循环 */
|
||||
private var fgRetried = false
|
||||
|
||||
private fun startForegroundCompat() {
|
||||
val notification = Notif.buildOngoing(this)
|
||||
// 必须显式传类型:specialUse 是 Android 14 才引入的,
|
||||
@@ -115,10 +118,22 @@ class TimerService : Service() {
|
||||
}
|
||||
runCatching {
|
||||
startForeground(Notif.ID_ONGOING, notification, type)
|
||||
}.recoverCatching {
|
||||
// 显式类型被拒时退回两参版本再试一次(读清单组合类型)
|
||||
startForeground(Notif.ID_ONGOING, notification)
|
||||
}.onSuccess {
|
||||
fgRetried = false
|
||||
}.onFailure {
|
||||
// 前台服务被系统拒绝(省电策略/权限)时退化为普通服务,
|
||||
// 计时本身仍在 TimerCore 里走,只是通知可能不显示。
|
||||
stopSelf()
|
||||
// EMUI 偶发拒绝(省电策略/启动节奏):延迟半秒自动重试一次,
|
||||
// 用户就不用"反复开关几次 APP"才能看到状态栏图标了;仍失败才放弃
|
||||
if (!fgRetried) {
|
||||
fgRetried = true
|
||||
handler.postDelayed({ startForegroundCompat() }, 500L)
|
||||
} else {
|
||||
// 前台服务被系统拒绝时退化为普通服务,
|
||||
// 计时本身仍在 TimerCore 里走,只是通知可能不显示。
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user