I'm trying to get the current location of the user of the app every 2 seconds.
I already use a foreground service with notification (can record location when app is in background). But when the app enters doze mode it will continue to give 3 locations after the screen turns black. But will then stop giving me locations updates.
I'm using FusedLocationProviderClient to get my current location.
I have a WakeLock to keep the service running and this works. To test this I have a corourtine running and I can do println("test") but when i want my location i get nothing.
fusedLocationProviderClient?.getCurrentLocation(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY, cancellationSource?.token)?
.addOnSuccessListener {
val loc = it
println("${loc.latitude},${loc.longitude}")
postNewLocation(loc)
}?.addOnCanceledListener {
println("cancel")
}
Just after I get my last location update I get this LocationEngineResult == null.
Does anyone know what I'm doing wrong? Any help or tips would be great.
For context I'm using Android 11 with targetsdkversion 30.
I'm a beginner in kotlin and this is for a schoolproject i have to complete.