Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android not receiving location updates when screen is locked

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.

like image 310
lintu Avatar asked Mar 04 '14 08:03

lintu


People also ask

Which method is best suited for getting location 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.

Which of the following method will used to receive location updates in the background even when the application is not active?

Once a location request is in place you can start the regular updates by calling requestLocationUpdates() .

Which API is used for continuously obtain the device location?

Using the fused location provider API, your app can request the last known location of the user's device.


1 Answers

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.

like image 65
Jimmy George Thomas Avatar answered Nov 04 '22 08:11

Jimmy George Thomas