From 36ee3ff2310b8c976f3c33ead9c428bbbc802a4d Mon Sep 17 00:00:00 2001 From: johan12345 Date: Wed, 26 Feb 2025 20:08:23 +0100 Subject: [PATCH] CarAppService: ignore if starting foreground service fails this happens on AAOS API 34+ due to https://developer.android.com/develop/background-work/services/fgs/restrictions-bg-start. However, the app still works even without the foreground service. --- .../java/net/vonforst/evmap/auto/CarAppService.kt | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt b/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt index 4cc8892c..129ce3ee 100644 --- a/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt +++ b/app/src/main/java/net/vonforst/evmap/auto/CarAppService.kt @@ -45,14 +45,20 @@ interface LocationAwareScreen { class CarAppService : androidx.car.app.CarAppService() { private val CHANNEL_ID = "car_location" private val NOTIFICATION_ID = 1000 + private val TAG = "CarAppService" private var foregroundStarted = false fun ensureForegroundService() { // we want to run as a foreground service to make sure we can use location - if (!foregroundStarted) { - createNotificationChannel() - startForeground(NOTIFICATION_ID, getNotification()) - foregroundStarted = true + try { + if (!foregroundStarted) { + createNotificationChannel() + startForeground(NOTIFICATION_ID, getNotification()) + foregroundStarted = true + Log.i(TAG, "Started foreground service") + } + } catch (e: SecurityException) { + Log.w(TAG, "Failed to start foreground service: ", e) } }