My app has to track the user continuously. For that I have a LocationListener
which is supposed to receive location updates continuously. The issue is when the screen is turned off, it does not receive any updates.
I have tried adding a partial wake lock:
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
mLocationClient = new LocationClient(ADS.getAppContext(), new LocationGatherer(), new LocationGatherer());
mLocationClient.connect();
PowerManager pm = (PowerManager) ADS.getAppContext().getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
wl.acquire();
But it still does not receive updates.
Allow only while using the app This option is the recommended option for most apps. Also known as "while-in-use" or "foreground only" access, this option was added in Android 10 and allows developers to retrieve location only while the app is actively being used.
Once a location request is in place you can start the regular updates by calling requestLocationUpdates() .
Using the fused location provider API, your app can request the last known location of the user's device.
Just bumped into the same problem. I too had the same code though it was within a Service. Strangely it did not work. Plugged in to my Laptop and ran a command mentioned in Android app high CPU usage, no services or wakelocks:
adb shell dumpsys power |grep -i wake
All of a sudden, location updates started arriving (the power button had been pressed and the screen was off).
I really have no clue as to what might have been the problem but that solved it for me.
As of now, receiving location updates even when the screen is turned off.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With