From cbb63be78a9d53b7dd4f07f1df4508f27e93fb0e Mon Sep 17 00:00:00 2001 From: Yu-ga <74749461+yuga-hashimoto@users.noreply.github.com> Date: Sun, 5 Apr 2026 21:35:20 +0900 Subject: [PATCH] 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. --- .../assistant/receiver/MediaButtonReceiver.kt | 16 +++++++++++++++- .../assistant/service/HotwordService.kt | 18 ++++++++++++++++-- .../assistant/service/NodeForegroundService.kt | 10 +++++++++- 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/openclaw/assistant/receiver/MediaButtonReceiver.kt b/app/src/main/java/com/openclaw/assistant/receiver/MediaButtonReceiver.kt index fb7808a..164f5b2 100644 --- a/app/src/main/java/com/openclaw/assistant/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/com/openclaw/assistant/receiver/MediaButtonReceiver.kt @@ -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() } diff --git a/app/src/main/java/com/openclaw/assistant/service/HotwordService.kt b/app/src/main/java/com/openclaw/assistant/service/HotwordService.kt index 3a060f9..9b416ec 100644 --- a/app/src/main/java/com/openclaw/assistant/service/HotwordService.kt +++ b/app/src/main/java/com/openclaw/assistant/service/HotwordService.kt @@ -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) + } } } diff --git a/app/src/main/java/com/openclaw/assistant/service/NodeForegroundService.kt b/app/src/main/java/com/openclaw/assistant/service/NodeForegroundService.kt index 09cfe0f..df25ffc 100644 --- a/app/src/main/java/com/openclaw/assistant/service/NodeForegroundService.kt +++ b/app/src/main/java/com/openclaw/assistant/service/NodeForegroundService.kt @@ -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) + } } /**