Skip to content

Commit e58669c

Browse files
Merge pull request #31 from icapps/feature/Wakelock
Ensure we keep a wakelock
2 parents bbc9fdb + 662861c commit e58669c

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
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: 24 additions & 8 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()
@@ -48,7 +49,8 @@ internal class LocationUpdatesService : Service() {
4849
* Callback for changes in location.
4950
*/
5051
private var locationCallback: LocationCallback? = null
51-
private var serviceHandler: Handler? = null
52+
53+
private var wakeLock: PowerManager.WakeLock? = null
5254

5355
/**
5456
* The current location.
@@ -64,11 +66,9 @@ internal class LocationUpdatesService : Service() {
6466
onNewLocation(locationResult.lastLocation)
6567
}
6668
}
69+
wakeLock = (getSystemService(Context.POWER_SERVICE) as PowerManager).newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "mijnmooiestraat:location_updates")
6770
createLocationRequest()
6871
getLastLocation()
69-
val handlerThread = HandlerThread(TAG)
70-
handlerThread.start()
71-
serviceHandler = Handler(handlerThread.looper)
7272

7373
if (SharedPrefsUtil.isTracking(this)) {
7474
startTracking()
@@ -77,14 +77,17 @@ internal class LocationUpdatesService : Service() {
7777

7878
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
7979
Logger.debug(TAG, "Service started")
80-
val startedFromNotification = intent?.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION,
81-
false) ?: false
80+
val startedFromNotification = intent?.getBooleanExtra(
81+
EXTRA_STARTED_FROM_NOTIFICATION,
82+
false
83+
) ?: false
8284

8385
// We got here because the user decided to remove location updates from the notification.
8486
if (startedFromNotification) {
8587
stopTracking()
8688
stopSelf()
8789
}
90+
8891
// Tells the system to try to recreate the service after it has been killed.
8992
return START_STICKY
9093
}
@@ -122,21 +125,28 @@ internal class LocationUpdatesService : Service() {
122125
// do nothing. Otherwise, we make this service a foreground service.
123126
if (!changingConfiguration && SharedPrefsUtil.isTracking(this)) {
124127
Logger.debug(TAG, "Starting foreground service")
128+
if (wakeLock?.isHeld != true) {
129+
wakeLock?.acquire(24 * 60 * 60 * 1000L)
130+
}
125131
NotificationUtil.startForeground(this, location)
126132
}
127133
return true // Ensures onRebind() is called when a client re-binds.
128134
}
129135

130136
override fun onDestroy() {
131137
Logger.debug(TAG, "Destroy")
132-
serviceHandler!!.removeCallbacksAndMessages(null)
138+
if (wakeLock?.isHeld == true) {
139+
wakeLock?.release()
140+
}
133141
}
134142

135143
/**
136144
* Makes a request for location updates. Note that in this sample we merely log the
137145
* [SecurityException].
138146
*/
139147
fun startTracking() {
148+
wakeLock?.acquire(24 * 60 * 60 * 1000L /*24 hours max */)
149+
140150
Logger.debug(TAG, "Requesting location updates")
141151
SharedPrefsUtil.saveIsTracking(this, true)
142152
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && ActivityCounter.isAppInBackground()) {
@@ -148,6 +158,9 @@ internal class LocationUpdatesService : Service() {
148158
try {
149159
fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper())
150160
} catch (unlikely: SecurityException) {
161+
if (wakeLock?.isHeld == true) {
162+
wakeLock?.release()
163+
}
151164
SharedPrefsUtil.saveIsTracking(this, false)
152165
Logger.error(TAG, "Lost location permission. Could not request updates. $unlikely")
153166
}
@@ -158,6 +171,9 @@ internal class LocationUpdatesService : Service() {
158171
* [SecurityException].
159172
*/
160173
fun stopTracking() {
174+
if (wakeLock?.isHeld == true) {
175+
wakeLock?.release();
176+
}
161177
Logger.debug(TAG, "Removing location updates")
162178
try {
163179
fusedLocationClient?.removeLocationUpdates(locationCallback)

0 commit comments

Comments
 (0)