cesarmax
cesarmax

Reputation: 434

does location manager keeps working when device goes to sleep?

My application requests for updates in a service on background when a boolean flag is set to true. If flag is set to true, then i acquire a PARTIAL_WAKE_LOCK to let my background service run. My questions are:

Upvotes: 4

Views: 8645

Answers (2)

AlexeyVMP
AlexeyVMP

Reputation: 2426

I've spent days trying to get WiFi and cell-based locations with locked screen with Android 6.0 on Nexus 6. And looks like the native android location service simple does not allow to do it. Once device got locked it still collects location update events for 10-15 minutes then stops to providing any of location updates.

In my case the solution was to switch from native Android location service to Google Play Services wrapper called com.google.android.gms.location: https://developers.google.com/android/reference/com/google/android/gms/location/package-summary

Yes, I know that some of Android devices lack of GMS, but for my application this is the only solution to perform.

It does not stop sending location updates even when in the background and device screen is locked.

Me personally prefer RxJava library to wrap this service into a stream (examples included): https://github.com/mcharmas/Android-ReactiveLocation

Upvotes: 0

slinden77
slinden77

Reputation: 3388

  1. no, it does not. More (very good) info here, including possible solutions/ hacks.

  2. regarding 1, I would have to make an educated guess and say it wouldn't make a difference

  3. from what I can gather, it doesn't make a difference which provider you are using for the updates, LocationManager.NETWORK_PROVIDER or LocationManager.GPS_PROVIDER.

in danger of going a bit OQ, I am a bit curious which kind of application would need to aquire a wake lock to keep a service running at all. As far as I know, having a wake lock doesn't ensure your Service keeps running. The only thing which ensures a Service keeps running is to have it in the foreground (Service.startForeground()). Otherwise the system still might kill the service, regardless if it aquired a wake lock or not. That being said, if it is running, it can do it's work with a Handler or something.

If you are using this approach, and I think you are based on the scenario, I would advise against it. Basically you are creating a service, have it run in the foreground (guess) AND you are aquiring a wake lock just to request for location updates when the screen is off. This seems a bit overkill.

There's a more efficient way, which has the benefit it has by far more accurate timing then the dreaded timing of Handler.postAtTime or Handler.postDelayed: AlarmManager.setRepeating(). Set type to ELAPSED_REALTIME_WAKEUP or RTC_WAKEUP so it will run if the device sleeps, then when the alarm event is fired and received by a BroadcastReceiver you will have to create, you could request for updates and handle other events.

If you're not using a Handler, but are merely requesting location updates, this approach still probably would be better, because it doesn't require you to have a running Service or to acquire a wake lock.

And it seems LocationManager.addProximityAlert() is the way to go here. Which is flawed as well (see 1)

Similar question here by the way: Android: GPS location updates when the cellphone is in sleep?

Upvotes: 5

Related Questions