fix: onStartCommand 编译错误 — return 位置冲突
- 将 try-when-return 结构拆分:onStartCommand 始终 return START_NOT_STICKY - STOP 逻辑抽为 handleStop() 独立方法,用 return 提前退出
This commit is contained in:
@@ -43,8 +43,8 @@ class AirDisplayService : Service() {
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
return try {
|
||||
Log.w(TAG, ">>> onStartCommand action=${intent?.action}")
|
||||
Log.w(TAG, ">>> onStartCommand action=${intent?.action}")
|
||||
try {
|
||||
when (intent?.action) {
|
||||
ACTION_START -> {
|
||||
Log.w(TAG, " build notification...")
|
||||
@@ -66,27 +66,27 @@ class AirDisplayService : Service() {
|
||||
manager.start()
|
||||
}
|
||||
}
|
||||
ACTION_STOP -> {
|
||||
Log.w(TAG, " 收到 STOP (started=$started)")
|
||||
if (started) {
|
||||
started = false
|
||||
scope.launch {
|
||||
Log.w(TAG, " manager.stop()...")
|
||||
manager.stop()
|
||||
Log.w(TAG, " manager.destroy()...")
|
||||
manager.destroy()
|
||||
Log.w(TAG, " stop/destroy 完成")
|
||||
}
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
}
|
||||
ACTION_STOP -> handleStop()
|
||||
}
|
||||
START_NOT_STICKY
|
||||
} catch (e: Exception) {
|
||||
Log.wtf(TAG, "FATAL in onStartCommand", e)
|
||||
START_NOT_STICKY
|
||||
}
|
||||
return START_NOT_STICKY
|
||||
}
|
||||
|
||||
private fun handleStop() {
|
||||
Log.w(TAG, " 收到 STOP (started=$started)")
|
||||
if (!started) return
|
||||
started = false
|
||||
scope.launch {
|
||||
Log.w(TAG, " manager.stop()...")
|
||||
manager.stop()
|
||||
Log.w(TAG, " manager.destroy()...")
|
||||
manager.destroy()
|
||||
Log.w(TAG, " stop/destroy 完成")
|
||||
}
|
||||
stopForeground(STOP_FOREGROUND_REMOVE)
|
||||
stopSelf()
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder = LocalBinder()
|
||||
|
||||
Reference in New Issue
Block a user