@@ -12,6 +12,7 @@ import android.os.Handler
12
12
import android.os.HandlerThread
13
13
import android.os.IBinder
14
14
import android.os.Looper
15
+ import android.os.PowerManager
15
16
import androidx.localbroadcastmanager.content.LocalBroadcastManager
16
17
import com.google.android.gms.location.FusedLocationProviderClient
17
18
import com.google.android.gms.location.LocationCallback
@@ -21,8 +22,8 @@ import com.google.android.gms.location.LocationServices
21
22
import com.icapps.background_location_tracker.flutter.FlutterBackgroundManager
22
23
import com.icapps.background_location_tracker.utils.ActivityCounter
23
24
import com.icapps.background_location_tracker.utils.Logger
24
- import com.icapps.background_location_tracker.utils.SharedPrefsUtil
25
25
import com.icapps.background_location_tracker.utils.NotificationUtil
26
+ import com.icapps.background_location_tracker.utils.SharedPrefsUtil
26
27
27
28
internal class LocationUpdatesService : Service () {
28
29
private val binder: IBinder = LocalBinder ()
@@ -50,6 +51,8 @@ internal class LocationUpdatesService : Service() {
50
51
private var locationCallback: LocationCallback ? = null
51
52
private var serviceHandler: Handler ? = null
52
53
54
+ private var wakeLock: PowerManager .WakeLock ? = null
55
+
53
56
/* *
54
57
* The current location.
55
58
*/
@@ -64,6 +67,7 @@ internal class LocationUpdatesService : Service() {
64
67
onNewLocation(locationResult.lastLocation)
65
68
}
66
69
}
70
+ wakeLock = (getSystemService(Context .POWER_SERVICE ) as PowerManager ).newWakeLock(PowerManager .PARTIAL_WAKE_LOCK , " mijnmooiestraat:location_updates" )
67
71
createLocationRequest()
68
72
getLastLocation()
69
73
val handlerThread = HandlerThread (TAG )
@@ -78,7 +82,7 @@ internal class LocationUpdatesService : Service() {
78
82
override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
79
83
Logger .debug(TAG , " Service started" )
80
84
val startedFromNotification = intent?.getBooleanExtra(EXTRA_STARTED_FROM_NOTIFICATION ,
81
- false ) ? : false
85
+ false ) ? : false
82
86
83
87
// We got here because the user decided to remove location updates from the notification.
84
88
if (startedFromNotification) {
@@ -122,6 +126,9 @@ internal class LocationUpdatesService : Service() {
122
126
// do nothing. Otherwise, we make this service a foreground service.
123
127
if (! changingConfiguration && SharedPrefsUtil .isTracking(this )) {
124
128
Logger .debug(TAG , " Starting foreground service" )
129
+ if (wakeLock?.isHeld != true ) {
130
+ wakeLock?.acquire(24 * 60 * 60 * 1000L )
131
+ }
125
132
NotificationUtil .startForeground(this , location)
126
133
}
127
134
return true // Ensures onRebind() is called when a client re-binds.
@@ -130,13 +137,18 @@ internal class LocationUpdatesService : Service() {
130
137
override fun onDestroy () {
131
138
Logger .debug(TAG , " Destroy" )
132
139
serviceHandler!! .removeCallbacksAndMessages(null )
140
+ if (wakeLock?.isHeld == true ) {
141
+ wakeLock?.release()
142
+ }
133
143
}
134
144
135
145
/* *
136
146
* Makes a request for location updates. Note that in this sample we merely log the
137
147
* [SecurityException].
138
148
*/
139
149
fun startTracking () {
150
+ wakeLock?.acquire(24 * 60 * 60 * 1000L /* 24 hours max */ )
151
+
140
152
Logger .debug(TAG , " Requesting location updates" )
141
153
SharedPrefsUtil .saveIsTracking(this , true )
142
154
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && ActivityCounter .isAppInBackground()) {
@@ -148,6 +160,9 @@ internal class LocationUpdatesService : Service() {
148
160
try {
149
161
fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback, Looper .myLooper())
150
162
} catch (unlikely: SecurityException ) {
163
+ if (wakeLock?.isHeld == true ) {
164
+ wakeLock?.release()
165
+ }
151
166
SharedPrefsUtil .saveIsTracking(this , false )
152
167
Logger .error(TAG , " Lost location permission. Could not request updates. $unlikely " )
153
168
}
@@ -158,6 +173,9 @@ internal class LocationUpdatesService : Service() {
158
173
* [SecurityException].
159
174
*/
160
175
fun stopTracking () {
176
+ if (wakeLock?.isHeld == true ) {
177
+ wakeLock?.release();
178
+ }
161
179
Logger .debug(TAG , " Removing location updates" )
162
180
try {
163
181
fusedLocationClient?.removeLocationUpdates(locationCallback)
0 commit comments