Skip to content

Commit c09f415

Browse files
Merge pull request #41 from jorre127/bugfix/fix-service-launching-background
fix service being launched in the background
2 parents 2ec9139 + aecb5f7 commit c09f415

File tree

5 files changed

+25
-8
lines changed

5 files changed

+25
-8
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: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import com.icapps.background_location_tracker.utils.ActivityCounter
2323
import com.icapps.background_location_tracker.utils.Logger
2424
import com.icapps.background_location_tracker.utils.NotificationUtil
2525
import com.icapps.background_location_tracker.utils.SharedPrefsUtil
26+
import java.io.PrintWriter
27+
import java.io.StringWriter
28+
29+
private const val timeOut = 24 * 60 * 60 * 1000L /*24 hours max */
2630

2731
internal class LocationUpdatesService : Service() {
2832
private val binder: IBinder = LocalBinder()
@@ -131,13 +135,23 @@ internal class LocationUpdatesService : Service() {
131135
// Called when the last client (MainActivity in case of this sample) unbinds from this
132136
// service. If this method is called due to a configuration change in MainActivity, we
133137
// 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)
138+
139+
try {
140+
if (!changingConfiguration && SharedPrefsUtil.isTracking(this)) {
141+
Logger.debug(TAG, "Starting foreground service")
142+
if (wakeLock?.isHeld != true) {
143+
wakeLock?.acquire(timeOut)
144+
}
145+
NotificationUtil.startForeground(this, location)
138146
}
139-
NotificationUtil.startForeground(this, location)
147+
} catch(e:Throwable) {
148+
val sw = StringWriter()
149+
val pw = PrintWriter(sw)
150+
e.printStackTrace(pw)
151+
pw.flush()
152+
Logger.error(sw.toString(),"onUnbind failed to execute");
140153
}
154+
141155
return true // Ensures onRebind() is called when a client re-binds.
142156
}
143157

@@ -153,7 +167,7 @@ internal class LocationUpdatesService : Service() {
153167
* [SecurityException].
154168
*/
155169
fun startTracking() {
156-
wakeLock?.acquire(24 * 60 * 60 * 1000L /*24 hours max */)
170+
wakeLock?.acquire(timeOut)
157171

158172
Logger.debug(TAG, "Requesting location updates")
159173
SharedPrefsUtil.saveIsTracking(this, true)

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:

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: background_location_tracker
22
description: A Flutter plugin that allows you to track the background location for Android & iOS
33
repository: https://github.com/icapps/flutter-background-location-tracker
4-
version: 1.3.0
4+
version: 1.3.1
55

66
environment:
77
sdk: ">=2.12.0 <3.0.0"

0 commit comments

Comments
 (0)