Skip to content

Commit 74efa36

Browse files
Ensure we keep a wakelock
1 parent bbc9fdb commit 74efa36

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package="com.icapps.background_location_tracker">
33

44
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
5-
5+
<uses-permission android:name="android.permission.WAKE_LOCK" />
66
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
77

88
<application>

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import android.os.Handler
1212
import android.os.HandlerThread
1313
import android.os.IBinder
1414
import android.os.Looper
15+
import android.os.PowerManager
1516
import androidx.localbroadcastmanager.content.LocalBroadcastManager
1617
import com.google.android.gms.location.FusedLocationProviderClient
1718
import com.google.android.gms.location.LocationCallback
@@ -21,8 +22,8 @@ import com.google.android.gms.location.LocationServices
2122
import com.icapps.background_location_tracker.flutter.FlutterBackgroundManager
2223
import com.icapps.background_location_tracker.utils.ActivityCounter
2324
import com.icapps.background_location_tracker.utils.Logger
24-
import com.icapps.background_location_tracker.utils.SharedPrefsUtil
2525
import com.icapps.background_location_tracker.utils.NotificationUtil
26+
import com.icapps.background_location_tracker.utils.SharedPrefsUtil
2627

2728
internal class LocationUpdatesService : Service() {
2829
private val binder: IBinder = LocalBinder()
@@ -50,6 +51,8 @@ internal class LocationUpdatesService : Service() {
5051
private var locationCallback: LocationCallback? = null
5152
private var serviceHandler: Handler? = null
5253

54+
private var wakeLock: PowerManager.WakeLock? = null
55+
5356
/**
5457
* The current location.
5558
*/
@@ -64,6 +67,7 @@ internal class LocationUpdatesService : Service() {
6467
onNewLocation(locationResult.lastLocation)
6568
}
6669
}
70+
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mijnmooiestraat:location_updates")
6771
createLocationRequest()
6872
getLastLocation()
6973
val handlerThread = HandlerThread(TAG)
@@ -78,7 +82,7 @@ internal class LocationUpdatesService : Service() {
7882
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
7983
Logger.debug(TAG, "Service started")
8084
val startedFromNotification = intent?.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
81-
false) ?: false
85+
false) ?: false
8286

8387
// We got here because the user decided to remove location updates from the notification.
8488
if (startedFromNotification) {
@@ -122,6 +126,9 @@ internal class LocationUpdatesService : Service() {
122126
// do nothing. Otherwise, we make this service a foreground service.
123127
if (!changingConfiguration && SharedPrefsUtil.isTracking(this)) {
124128
Logger.debug(TAG, "Starting foreground service")
129+
if (wakeLock?.isHeld != true) {
130+
wakeLock?.acquire(24 * 60 * 60 * 1000L)
131+
}
125132
NotificationUtil.startForeground(this, location)
126133
}
127134
return true // Ensures onRebind() is called when a client re-binds.
@@ -130,13 +137,18 @@ internal class LocationUpdatesService : Service() {
130137
override fun onDestroy() {
131138
Logger.debug(TAG, "Destroy")
132139
serviceHandler!!.removeCallbacksAndMessages(null)
140+
if (wakeLock?.isHeld == true) {
141+
wakeLock?.release()
142+
}
133143
}
134144

135145
/**
136146
* Makes a request for location updates. Note that in this sample we merely log the
137147
* [SecurityException].
138148
*/
139149
fun startTracking() {
150+
wakeLock?.acquire(24 * 60 * 60 * 1000L /*24 hours max */)
151+
140152
Logger.debug(TAG, "Requesting location updates")
141153
SharedPrefsUtil.saveIsTracking(this, true)
142154
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && ActivityCounter.isAppInBackground()) {
@@ -148,6 +160,9 @@ internal class LocationUpdatesService : Service() {
148160
try {
149161
fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
150162
} catch (unlikely: SecurityException) {
163+
if (wakeLock?.isHeld == true) {
164+
wakeLock?.release()
165+
}
151166
SharedPrefsUtil.saveIsTracking(this, false)
152167
Logger.error(TAG, "Lost location permission. Could not request updates. $unlikely")
153168
}
@@ -158,6 +173,9 @@ internal class LocationUpdatesService : Service() {
158173
* [SecurityException].
159174
*/
160175
fun stopTracking() {
176+
if (wakeLock?.isHeld == true) {
177+
wakeLock?.release();
178+
}
161179
Logger.debug(TAG, "Removing location updates")
162180
try {
163181
fusedLocationClient?.removeLocationUpdates(locationCallback)

0 commit comments

Comments
 (0)