Colota - Android GPS Location tracker
Handle ForegroundServiceStartNotAllowedException on Android 12+ device (#221)
Max Dietrich4ba75197f7ce
diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/BootReceiver.kt b/apps/mobile/android/app/src/main/java/com/colota/service/BootReceiver.ktindex e6dffd4c..305842d7 100644--- a/apps/mobile/android/app/src/main/java/com/colota/service/BootReceiver.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/service/BootReceiver.kt@@ -102,6 +102,9 @@ class LocationBootReceiver : BroadcastReceiver() {} catch (e: SecurityException) {AppLogger.e(TAG, "Permission denied when starting service", e)+ } catch (e: IllegalStateException) {+ // ForegroundServiceStartNotAllowedException on Android 12++ AppLogger.e(TAG, "Cannot start foreground service from background", e)} catch (e: Exception) {AppLogger.e(TAG, "Error restarting service on boot", e)}diff --git a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.ktindex 53f2efef..e9e58e07 100644--- a/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt+++ b/apps/mobile/android/app/src/main/java/com/colota/service/LocationForegroundService.kt@@ -145,12 +145,18 @@ class LocationForegroundService : Service() {val initialTitle = notificationHelper.buildTitle(if (::profileManager.isInitialized) profileManager.getActiveProfileName() else null)- ServiceCompat.startForeground(- this,- NotificationHelper.NOTIFICATION_ID,- notificationHelper.buildTrackingNotification(initialTitle, initialStatus),- ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION- )+ try {+ ServiceCompat.startForeground(+ this,+ NotificationHelper.NOTIFICATION_ID,+ notificationHelper.buildTrackingNotification(initialTitle, initialStatus),+ ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION+ )+ } catch (e: Exception) {+ AppLogger.e(TAG, "Cannot start foreground service - background start restricted", e)+ stopSelf()+ return START_NOT_STICKY+ }if (!isLightweight) {dbHelper.saveSetting("tracking_enabled", "true")