Skip to content

Commit a203220

Browse files
committed
fix service being launched in the background
1 parent 2ec9139 commit a203220

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ android {
3232
}
3333
defaultConfig {
3434
minSdkVersion 16
35+
targetSdkVersion 33
3536
}
3637
lintOptions {
3738
disable 'InvalidPackage'
@@ -42,4 +43,5 @@ dependencies {
4243
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
4344
implementation 'com.google.android.gms:play-services-location:20.0.0'
4445
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.1.0'
46+
implementation "androidx.core:core-ktx:1.9.0"
4547
}

android/src/main/kotlin/com/icapps/background_location_tracker/service/LocationUpdatesService.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,20 @@ internal class LocationUpdatesService : Service() {
131131
// Called when the last client (MainActivity in case of this sample) unbinds from this
132132
// service. If this method is called due to a configuration change in MainActivity, we
133133
// do nothing. Otherwise, we make this service a foreground service.
134-
if (!changingConfiguration && SharedPrefsUtil.isTracking(this)) {
135-
Logger.debug(TAG, "Starting foreground service")
136-
if (wakeLock?.isHeld != true) {
137-
wakeLock?.acquire(24 * 60 * 60 * 1000L)
134+
135+
try{
136+
if (!changingConfiguration && SharedPrefsUtil.isTracking(this)) {
137+
Logger.debug(TAG, "Starting foreground service")
138+
if (wakeLock?.isHeld != true) {
139+
wakeLock?.acquire(24 * 60 * 60 * 1000L)
140+
}
141+
NotificationUtil.startForeground(this, location)
138142
}
139-
NotificationUtil.startForeground(this, location)
140143
}
144+
catch(e:Exception) {
145+
Logger.error(e.message ?: "","onUnbind failed to execute", );
146+
}
147+
141148
return true // Ensures onRebind() is called when a client re-binds.
142149
}
143150

android/src/main/kotlin/com/icapps/background_location_tracker/utils/NotificationUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ internal object NotificationUtil {
7878
.setContentTitle(title)
7979
.setContentText(text)
8080
.setContentIntent(clickPendingIntent)
81+
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
8182
if (SharedPrefsUtil.isCancelTrackingActionEnabled(context)) {
8283
builder.addAction(0, SharedPrefsUtil.getCancelTrackingActionText(context), cancelTrackingIntent)
8384
}

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ packages:
2121
path: ".."
2222
relative: true
2323
source: path
24-
version: "1.2.0"
24+
version: "1.3.0"
2525
boolean_selector:
2626
dependency: transitive
2727
description:

0 commit comments

Comments
 (0)