does location manager keeps working when device goes to sleep? - android

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:
Since I requested for updates from location manager ( i don't manually request updates but subscribed for locationManager.requestLocationUpdates )... does the locationManager keep working as normal/usual even if device goes to sleep with PARTIAL_WAKE_LOCK ?
I've read there is a WifiLock -> WIFI_MODE_SCAN_ONLY that I'm not acquiring. Since location manager uses wifi scans to detect location through wifi hotspots, should I acquire this as well ?
What about gps location updates when device goes to sleep ?

no, it does not. More (very good) info here, including possible solutions/ hacks.
regarding 1, I would have to make an educated guess and say it wouldn't make a difference
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?

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

Related

Android background service location updates and Doze

My app lets people record routes when driving. To ensure locations are actually recorded, i use a service so that they are recorded even if the activity is killed. To avoid the device being put into Doze, which would mean not getting frequent location updates, i hold a wakelock.
Are these steps sufficient? Or do i need to explicitly disable battery optimization ( https://developer.android.com/training/monitoring-device-state/doze-standby.html ) to ensure i dont lose out on location updates? According to the docs, my app seems like an "acceptable" use-case for that.
Foreground services are not affected by doze - if you want to constantly be running and getting high accuracy location, you should be a foreground service.

How does Doze/Standby feature affect location updates?

Does anyone know if a whitelisted app that is holding a partial wake-lock can receive location updates while the device is in Doze or the app is in Standby?
The Android docs (http://developer.android.com/training/monitoring-device-state/doze-standby.html) only state that CPU and network are deferred and do not mention any affect on location updates (i.e. from the LocationManager). Based on this, it would seem that location updates are not affected by Doze/Standby, so if an app is holding a partial wake-lock (to keep the CPU running) then the app should be able to receive location updates.
I've implemented this and tested it and found that an app does not appear to receive location updates in Doze even though it has a partial wake-lock and is whitelisted. Interestingly, when I left the device on my desk overnight running the app with the wake-lock, the battery was nearly empty in the morning, but when repeating with the app without the wake-lock, the battery was nearly full in the morning. So it does appear that the app was running the whole time, but was not receiving location updates.
A little extra background: The app is used for fleet tracking, so we're trying to figure out if there is anyway for an app to have a background service running that reliably receives location updates when running on battery power.
Thanks!
-Tom B.
Yes, this seems to be another undocumented restriction in Doze mode, as my test logs also show. My guess was that some hardware features like GPS are generally turned off in Doze, but maybe it's the LocationManager being disabled altogether.
As this might just be a severe lack of documentation, the battery drain you mention should definitely not happen in Doze, as that's exactly what Doze is meaning to prevent in the first place.
You may want to file a bug report about all this at https://code.google.com/p/android/issues/list
For getting location updates when phone is in idle mode, app should be whitelisted, had partial wake-lock and also should not be on the same locatoin (if you are in the same location you don't need updates). I tested this with Mock Locations, which was giving fake location while phone was on my desk.
In my experience location updates are still received when in Doze but only when you use a foreground notification together with the methods you described. Network calls don't work.
Be aware of some stackoverflow posts indicating to just use the permission REQUEST_IGNORE_BATTERY_OPTIMIZATIONS. Your app will be rejected from the play store when you define this permission in your manifest due to "device & network abuse" https://play.google.com/about/privacy-security/device-network-abuse/.
There's also a way to manually request this permission with an intent. I don't know if it's also prohibited that way. The whole whitelisting principle is a little vague to me.

Location tracking like in fitness apps

My goal
Get user location updates for quite long time (e.g. 8 hours) with quite high frequency (e.g. every 30 seconds) even when the application is not running in foreground (meaning the activity where the location tracking was started might be destroyed).
Issue
I've found many articles regarding location tracking in Android apps.
The newer ones usually explain how to use Google Play Services' location APIs.
The problem is that in most cases, they demonstrate getting location updates in co-operation with Activity and LocationListener. For example in Google's tutorial. It's obvious this is not what I need. The only approach for long-running background location updates was based on periodical starting (via AlarmManager) of a service that run until it got accurate enough location update. However, this approach doesn't seem right for the frequency I need.
So, I ended up with custom idea how to solve the topic, but your critique would be welcomed. Here it is:
Idea of a possible solution
MainActivity - used just for starting/stopping the tracking by starting/stopping the MonitoringService
MonitoringService - a foreground service, where the whole connecting to LocationServices from Google Play services happens. Also, once connected a requestLocationUpdates method is called, but its variant with PendingIntent object. The pending intent contains intent invoking MyWakefulBroadcastReceiver class. Also, the service holds a partial wake_lock to prevent the device going to sleep and interrupting receiving of the location updates. Here, I'm not sure if holding the wake lock really helps.
MyWakefulBroadcastReceiver - extends WakefulBroadcastReceiver, just starts LocationProcessingIntentService via the startWakefulService
LocationProcessingIntentService - processes the location update passed via intent to it. Network communication performed here. Therefore, it is done in separate service and not directly in the MonitoringService
Currently, I have the solution described above implemented without acquiring the partial wake lock in the MonitoringService. When connected to debugger in Android Studio, I see the processing of the location updates work. However, I'm not sure what it will do after e.g. 4 hours of running without being connected to laptop (like it is for the debugging purposes)
Questions
Is this approach OK from architecture/performance/battery life point of view?
Should I use the wake lock for being sure the device won't go to sleep?
If answer to 2. is yes, do I still need to use WakefulBroadcastReceiver for starting the IntentService processing the location updates?
Any other recommendations?
What you have is mostly fine. You don't need the MonitoringService. The PendingIntent will wake your BroadcastReceiver, regardless of the state of your app. You don't need a permanent wake-lock. Just use the PendingIntent in your setup Activity to requestLocationUpdates.
Remember that the location services are already running in the background (as long as the user has enabled location services), so you don't need to run your permanent wake-lock service also (you can wake-lock after BroadcastReceiver#onReceive(), but don't forget to release the wake-lock after processing).
The only time you might have to worry about the PendingIntent not waking your BroadcastReceiver is if the user force-stops your app. In that case, you could choose to respect the user's decision. You can also explore using intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); which should wake your BroadcastReceiver from the stopped state, but I haven't tested this flag in this scenario.

Is it required to get a wakelock for an Android service with an active LocationListener?

In my application, I have a service that has an active LocationListener and sends the location over the air. When the service is running, the phone never goes to sleep, and it keeps sending the locations, even though I don't have a wakelock. This is exactly what I want, but I was wondering if that's the expected behavior. I couldn't find any information on this in the SDK documentation. Can I rely on it, or is it safer to get my own wakelock ?
Running location services continuously is battery consuming.User do not like apps that consume their battery and they wont think twice before deleting the app.Best idea is to wake up service only when needed.
Please check this link for more information on battery usage trade offs

Android LocationListener leave on while phone asleep

I set an alarm with the flag RTC_WAKEUP to run a IntentService every 30 seconds to transmit location updates to a server. I'm planning to change the flag to RTC so it won't wake up the phone and just run when another process wake ups the phone. If I leave a LocationListener registered, will it still listen for location updates while the phone is asleep?
Yes - working location service has it's own wake lock. However better approach is manually set proper wake lock in your broadcast receiver. Please consider some optimization - sending data over network every 30s will drain battery.
You have multiple problems here.
I set an alarm with the flag RTC_WAKEUP to run a IntentService every 30 seconds to transmit location updates to a server.
First, you may not even get your first fix within 30 seconds, particularly if you are using GPS. You need to take into account that you may never get a fix (e.g., the user is in an underground location).
Second, please allow this figure to be user-configurable, including an option for "I'll upload the data manually please". As #piotrpo indicates, this is a significant drain on the battery. In fact, if you're using GPS, I doubt the battery will last more than a couple of hours.
Third, an IntentService will not work well in this case, because the IntentService will shut down before your fix arrives. At best, you'll leak memory. At worst, you won't get your fix, because Android terminates your process.
A better solution for doing background location checks is to use a regular Service, not an IntentService. The regular Service would register the LocationListener in onStartCommand(), plus arrange for a timeout notification (e.g., AlarmManager and set()) in case a fix is not available. When the fix arrives, run an AsyncTask to do your upload. When the AsyncTask completes, or if the timeout arrives and you did not get a fix, unregister the listener and call stopSelf() to shut down the service. Along the way, you will need to maintain your own WakeLock, to keep the device awake while all of this is going on.
For an example of most of this (minus the server upload part), see my LocationPoller.
If you are dead-set on this occurring every 30 seconds or so, you may as well not bother with AlarmManager at all. You would have to have an everlasting service, running all the time, with a permanent WakeLock and a permanent LocationListener. When fixes arrive in onLocationChanged(), upload them if they are more than 30 seconds from the previous one. And, be sure to wear a flame-retardant suit when you release the app, as those who run it may not like the results much.

Categories

Resources