From abbc61d4193eb1e0aaf96b789ee274cad2858d1f Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Mon, 25 Dec 2023 21:50:55 +0100 Subject: [PATCH] TrackRecordingService: start via ServiceCompat. --- build.gradle | 1 + .../services/TrackRecordingService.java | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index 4b7ec7406..37778f45d 100644 --- a/build.gradle +++ b/build.gradle @@ -133,6 +133,7 @@ dependencies { implementation 'androidx.gridlayout:gridlayout:1.0.0' implementation 'com.google.android.material:material:1.11.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.core:core:1.12.0' implementation 'androidx.core:core-splashscreen:1.0.1' implementation 'androidx.mediarouter:mediarouter:1.6.0' diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java index 6c24c7de9..29cedf294 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingService.java @@ -30,6 +30,7 @@ import android.widget.Toast; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import androidx.core.app.ServiceCompat; import androidx.lifecycle.LiveData; import androidx.lifecycle.MutableLiveData; @@ -111,7 +112,7 @@ public class TrackRecordingService extends Service implements TrackPointCreator. recordingDataObservable = new MutableLiveData<>(NOT_RECORDING); trackPointCreator = new TrackPointCreator(this); - trackRecordingManager = new TrackRecordingManager(this, trackPointCreator, this , handler); + trackRecordingManager = new TrackRecordingManager(this, trackPointCreator, this, handler); voiceAnnouncementManager = new VoiceAnnouncementManager(this); notificationManager = new TrackRecordingServiceNotificationManager(this); @@ -213,19 +214,15 @@ public class TrackRecordingService extends Service implements TrackPointCreator. Log.i(TAG, "startSensors"); wakeLock = SystemUtils.acquireWakeLock(this, wakeLock); trackPointCreator.start(this, handler); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { - if (!PermissionRequester.RECORDING.hasPermission(this)) { - Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show(); - return; - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { + if (!PermissionRequester.RECORDING.hasPermission(this)) { + Toast.makeText(this, R.string.permission_recording_failed, Toast.LENGTH_LONG).show(); + return; } - - startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE); - } else { - startForeground(TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this)); } + + ServiceCompat.startForeground(this, TrackRecordingServiceNotificationManager.NOTIFICATION_ID, notificationManager.setGPSonlyStarted(this), ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION + ServiceInfo.FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE); } public void endCurrentTrack() {