@@ -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 ()
@@ -48,7 +49,8 @@ internal class LocationUpdatesService : Service() {
48
49
* Callback for changes in location.
49
50
*/
50
51
private var locationCallback: LocationCallback ? = null
51
- private var serviceHandler: Handler ? = null
52
+
53
+ private var wakeLock: PowerManager .WakeLock ? = null
52
54
53
55
/* *
54
56
* The current location.
@@ -64,11 +66,9 @@ internal class LocationUpdatesService : Service() {
64
66
onNewLocation(locationResult.lastLocation)
65
67
}
66
68
}
69
+ wakeLock = (getSystemService(Context .POWER_SERVICE ) as PowerManager ).newWakeLock(PowerManager .PARTIAL_WAKE_LOCK , " mijnmooiestraat:location_updates" )
67
70
createLocationRequest()
68
71
getLastLocation()
69
- val handlerThread = HandlerThread (TAG )
70
- handlerThread.start()
71
- serviceHandler = Handler (handlerThread.looper)
72
72
73
73
if (SharedPrefsUtil .isTracking(this )) {
74
74
startTracking()
@@ -77,14 +77,17 @@ internal class LocationUpdatesService : Service() {
77
77
78
78
override fun onStartCommand (intent : Intent ? , flags : Int , startId : Int ): Int {
79
79
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
82
84
83
85
// We got here because the user decided to remove location updates from the notification.
84
86
if (startedFromNotification) {
85
87
stopTracking()
86
88
stopSelf()
87
89
}
90
+
88
91
// Tells the system to try to recreate the service after it has been killed.
89
92
return START_STICKY
90
93
}
@@ -122,21 +125,28 @@ internal class LocationUpdatesService : Service() {
122
125
// do nothing. Otherwise, we make this service a foreground service.
123
126
if (! changingConfiguration && SharedPrefsUtil .isTracking(this )) {
124
127
Logger .debug(TAG , " Starting foreground service" )
128
+ if (wakeLock?.isHeld != true ) {
129
+ wakeLock?.acquire(24 * 60 * 60 * 1000L )
130
+ }
125
131
NotificationUtil .startForeground(this , location)
126
132
}
127
133
return true // Ensures onRebind() is called when a client re-binds.
128
134
}
129
135
130
136
override fun onDestroy () {
131
137
Logger .debug(TAG , " Destroy" )
132
- serviceHandler!! .removeCallbacksAndMessages(null )
138
+ if (wakeLock?.isHeld == true ) {
139
+ wakeLock?.release()
140
+ }
133
141
}
134
142
135
143
/* *
136
144
* Makes a request for location updates. Note that in this sample we merely log the
137
145
* [SecurityException].
138
146
*/
139
147
fun startTracking () {
148
+ wakeLock?.acquire(24 * 60 * 60 * 1000L /* 24 hours max */ )
149
+
140
150
Logger .debug(TAG , " Requesting location updates" )
141
151
SharedPrefsUtil .saveIsTracking(this , true )
142
152
if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .O && ActivityCounter .isAppInBackground()) {
@@ -148,6 +158,9 @@ internal class LocationUpdatesService : Service() {
148
158
try {
149
159
fusedLocationClient?.requestLocationUpdates(locationRequest, locationCallback, Looper .myLooper())
150
160
} catch (unlikely: SecurityException ) {
161
+ if (wakeLock?.isHeld == true ) {
162
+ wakeLock?.release()
163
+ }
151
164
SharedPrefsUtil .saveIsTracking(this , false )
152
165
Logger .error(TAG , " Lost location permission. Could not request updates. $unlikely " )
153
166
}
@@ -158,6 +171,9 @@ internal class LocationUpdatesService : Service() {
158
171
* [SecurityException].
159
172
*/
160
173
fun stopTracking () {
174
+ if (wakeLock?.isHeld == true ) {
175
+ wakeLock?.release();
176
+ }
161
177
Logger .debug(TAG , " Removing location updates" )
162
178
try {
163
179
fusedLocationClient?.removeLocationUpdates(locationCallback)
0 commit comments