Fix background service start crashes (#469)

Wraps startService calls in MediaButtonReceiver, HotwordService, and NodeForegroundService with try-catch for both IllegalStateException and SecurityException. Adds broadcast fallback for service start failures.
This commit is contained in:
Yu-ga
2026-04-05 21:35:20 +09:00
committed by GitHub
parent 8ac820b788
commit cbb63be78a
3 changed files with 40 additions and 4 deletions
@@ -44,7 +44,21 @@ class MediaButtonReceiver : BroadcastReceiver() {
val serviceIntent = Intent(context, OpenClawAssistantService::class.java).apply {
action = OpenClawAssistantService.ACTION_SHOW_ASSISTANT
}
context.startService(serviceIntent)
try {
context.startService(serviceIntent)
} catch (e: IllegalStateException) {
Log.w(TAG, "Background start failed, falling back to broadcast", e)
val broadcastIntent = Intent(OpenClawAssistantService.ACTION_SHOW_ASSISTANT).apply {
setPackage(context.packageName)
}
context.sendBroadcast(broadcastIntent)
} catch (e: SecurityException) {
Log.w(TAG, "Background start failed, falling back to broadcast", e)
val broadcastIntent = Intent(OpenClawAssistantService.ACTION_SHOW_ASSISTANT).apply {
setPackage(context.packageName)
}
context.sendBroadcast(broadcastIntent)
}
// Absorb the event so it doesn't reach the media player
abortBroadcast()
}
@@ -648,8 +648,22 @@ class HotwordService : Service(), VoskRecognitionListener {
val intent = Intent(this@HotwordService, OpenClawAssistantService::class.java).apply {
action = OpenClawAssistantService.ACTION_SHOW_ASSISTANT
}
startService(intent)
Log.e(TAG, "startService ACTION_SHOW_ASSISTANT called")
try {
startService(intent)
Log.e(TAG, "startService ACTION_SHOW_ASSISTANT called")
} catch (e: IllegalStateException) {
Log.w(TAG, "Background start failed, falling back to broadcast", e)
val broadcastIntent = Intent(OpenClawAssistantService.ACTION_SHOW_ASSISTANT).apply {
setPackage(packageName)
}
sendBroadcast(broadcastIntent)
} catch (e: SecurityException) {
Log.w(TAG, "Background start failed, falling back to broadcast", e)
val broadcastIntent = Intent(OpenClawAssistantService.ACTION_SHOW_ASSISTANT).apply {
setPackage(packageName)
}
sendBroadcast(broadcastIntent)
}
}
}
@@ -281,7 +281,15 @@ class NodeForegroundService : Service() {
fun stop(context: Context) {
val intent = Intent(context, NodeForegroundService::class.java).setAction(ACTION_STOP)
context.startService(intent)
try {
context.startService(intent)
} catch (e: IllegalStateException) {
android.util.Log.w(TAG, "Failed to send ACTION_STOP via startService, falling back to stopService", e)
context.stopService(intent)
} catch (e: SecurityException) {
android.util.Log.w(TAG, "Failed to send ACTION_STOP via startService, falling back to stopService", e)
context.stopService(intent)
}
}
/**