From 4da0599f0e75bcaabc340573ed1da2982caf1cad Mon Sep 17 00:00:00 2001 From: Albert Vaca Cintora Date: Sat, 29 Jul 2023 11:22:23 +0200 Subject: [PATCH] Fix crash on start in API 31+ Sometimes we can't start the foreground service at boot for some reason. I have a couple untested theories: either the phone is slow at boot and we don't get to call startForeground in time, or the user has never started the app before and we are not allowed to start it or to create a notification. --- src/org/kde/kdeconnect/KdeConnectBroadcastReceiver.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/org/kde/kdeconnect/KdeConnectBroadcastReceiver.java b/src/org/kde/kdeconnect/KdeConnectBroadcastReceiver.java index b2278c0f..74129ea0 100644 --- a/src/org/kde/kdeconnect/KdeConnectBroadcastReceiver.java +++ b/src/org/kde/kdeconnect/KdeConnectBroadcastReceiver.java @@ -28,7 +28,12 @@ public class KdeConnectBroadcastReceiver extends BroadcastReceiver { break; case Intent.ACTION_BOOT_COMPLETED: Log.i("KdeConnect", "KdeConnectBroadcastReceiver"); - BackgroundService.Start(context); + try { + BackgroundService.Start(context); + } catch (IllegalStateException e) { // To catch ForegroundServiceStartNotAllowedException + Log.w("BroadcastReceiver", "Couldn't start the foreground service."); + e.printStackTrace(); + } break; case WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION: case WifiManager.WIFI_STATE_CHANGED_ACTION: