mirror of
https://github.com/yuga-hashimoto/openclaw-assistant.git
synced 2026-08-14 08:35:19 +00:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user