I´m currently developing an app which is working as a proximity alert, using the network provider.
Running several tests, I noticed that my phone sometimes is not receiving updates when the screen is turned off.
Is LocationManager somehow affected by the phone´s standby?
Having reached my set destination, I turned my screen on and after some seconds (I request updates about every 60 seconds) my app suddenly gets a fresh and correct update. Especially when running Maps or other apps which use the network provider, my app does receive location updates more reliably.
Thanks for your answer in advance,
Jonathan
I didn´t find a solution but the problem. It is a nasty Android Bug, which prevents the system to send location updates with new coordinates when the device is in wakelock.
See here: Issue on Google Code
I hope this might help anybody out there to solve some problems.
When using LocationManager's addProximityAlert() you have to be aware that if the screen goes to sleep the checks for proximity alerts only happen once every 4 minutes in order to conserve battery life.
Related
I have implemented a foreground service to get GPS updates for longer runs, like 3 hours, when the app is in background. It is our business requirement. But the app stops getting updates after a few minutes. I see the "location icon" on top bar go disappear, even though my foreground service notification is still there.
I have also excluded my app from Battery Optimization list from the settings, so that Android doesn't kill my app. It helps to keep gps updates alive for about half an hour only, then again the location icon goes away and i stop getting gps updates.
What can I do to keep it always ON?
I am posting this answer based on my short comment and OP request
You can start that service by separate process by adding android:process=":FusedLocationService" in your Service component in manifest.xml.
I had done this and work in most of the device except some of the manufactures like Honor and other because they customized Android AOSP for better user experience
https://developer.android.com/about/versions/oreo/background-location-limits.html
Apart form this you can use JobScheduler to fetch location data after a particular interval (approx 15 mins)
You can build logic a/c to OS version as well.
And while fetching the location use FLP (FusedLocationProvider)
Hope this will help, But after all this depend on Mobile brand(like ONE PLUS) you will face the issues. You need to handle these scenario as well.
I have been testing an app for many months which uses the Android Geofencing API to track enter/exit events. It works flawlessly about 99% of the time, but there are random times when the geofences simply do not trigger.
This cannot be an issue caused by my application failing to keep the geofences loaded, because whenever the fences fail to trigger, opening a completely separate GPS application, ie google maps, immediately causes my application to trigger the geofence event that it had failed to.
I am handling the reloading of the fences through a broadcast receiver on BOOT_COMPLETED and location.PROVIDER_CHANGED.
The fence radius is reasonably large, over 1 mile.
The app is not being put to sleep or under power management.
Users and personal testing show this is never due to location services being disabled manually.
There is no dwell and the fences never expire.
As I said, the geofences work flawlessly 99% of the time, and the other 1% simply opening another gps application immediately triggers the fences. I'm trying to figure out what I can do to eliminate the failing 1%, hopefully something short of constantly polling location (which makes the geofencing api pointless anyway).
Update: (Limited testing sample size of 1)On a device running Android Oreo, this issue seems to have become exacerbated to the point of geofences never triggering unless a secondary gps app is launched...
I don't know if this will help anybody but with the Background limitations starting from Android O the geofences work as you described, only opening apps that get the current location of the user will trigger the geofences.
You can create a Foreground service and poll the location from the Fused Location Provider in order to make Geofences to actually work.
I hope someone else found a better solution.
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.
I'm trying to find out if the Google Play Services geofencing and location system can be practically used for background location monitoring.
An example scenario is that I have the phone in my pocket, and enter a geofenced area - will the event handler in my app be triggered so I can deal with the event as appropriate, or is the system only intended to be used whilst the phone is awake and the app in the foreground?
I've been banging away at the example code, and so far haven't managed to make it work in this way - or find docs fully addressing this - it seems to be too new for people to be using it much.
Obviously the phone doesn't use GPS continually whilst sleeping, or the battery would run down quickly, but I'm wondering if the broad network location is monitored and used for this purpose (or to discover if the device is within short distance of a geofence, to know that it's worth polling the GPS periodically to find if it has been crossed.)
This answer (by Commonsware) to another question seems to answer the general thrust of the question, though more testing is required to answer some of the more detailed aspects about background location availability, accuracy and geofence reliability.
Commonsware
Geofence if implemented properly will work even when your app is in background and it can be handled from ReceiveTransitionsIntentService In the Service you can create notification and alert the Users to bring back to your APP.
But As per the Documentation, If the Location Services is Disabled by the User on the Device your Registered Geofences will be removed and you have to Re-add all the Geofences. (NOT SURE HOW CAN WE ADD THE GEOFENCES BACK in BACKGROUND DID NOT FIGURE OUT THE SOLUTION YET FOR THIS)
Also in other cases like when the Device is Switched Off / Battery is Drained / Restarted Most probably the Geofences are Removed and we have to reset the Geofences back (However i cannot confirm this as have not found this anywhere in documentation but while testing i have figured out this issue)
I've created an android app that is so far working quite well. It contains a local service which is responsible for getting the users location every couple of minutes and relaying that via UDP to a collection server which is in turned displayed on a web application map.
The app needs to be constantly running unless the user explicitly signs out of the application. Currently, it seems the OS is shutting down the service and restarting it as needed. I can see this because normally it would report its location consistently every 2 minutes. Instead, there are gaps in these intervals.
So I'm looking for some advice on how I can write a service which (at the battery's expense unfortunately) can stay running always and pool the location service just enough to send accurate fixes at a 2 minute interval.
I have a well working code base so far, so if demonstrating where I currently am with code would help, don't hesitate to ask.
Thanks in advance to those that lend a hand.
Your best bet is to use the AlarmManager to wake up every two minutes, do some processing and quickly go back to sleep.
If you think you're hardcore, you could even use a wake lock, but that will keep the phone's CPU turned on persistently without mercy. You don't want that.