fix(service): Update foreground service type and error handling (#3246)

Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com>
This commit is contained in:
James Rich 2025-09-29 15:59:00 -05:00 committed by GitHub
parent 38332b346e
commit 6abe0124d2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -104,6 +104,7 @@ import org.meshtastic.core.model.util.toPIIString
import org.meshtastic.core.prefs.mesh.MeshPrefs import org.meshtastic.core.prefs.mesh.MeshPrefs
import org.meshtastic.core.prefs.ui.UiPrefs import org.meshtastic.core.prefs.ui.UiPrefs
import org.meshtastic.core.strings.R import org.meshtastic.core.strings.R
import timber.log.Timber
import java.util.Random import java.util.Random
import java.util.UUID import java.util.UUID
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
@ -389,25 +390,19 @@ class MeshService :
this, this,
MeshServiceNotifications.SERVICE_NOTIFY_ID, MeshServiceNotifications.SERVICE_NOTIFY_ID,
notification, notification,
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (hasLocationPermission()) { if (hasLocationPermission()) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE or
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
} else { } else {
ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE
} }
} else { } else {
0 0 // No specific type needed for older Android versions
}, },
) )
} catch (ex: SecurityException) { } catch (ex: Exception) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { Timber.e(ex, "Error starting foreground service")
errormsg(
"startForeground failed, likely due to missing POST_NOTIFICATIONS permission on Android 13+",
ex,
)
} else {
errormsg("startForeground failed", ex)
}
return START_NOT_STICKY return START_NOT_STICKY
} }
return if (!wantForeground) { return if (!wantForeground) {