I have been trying consistent location tracking code in my application.
But it always fails in doze mode and some devices. Location tracking does not work smoothly in all types of android devices
Can anyone tell me a perfect solution or code to track user location irrespective of the state of device and it should work on all kind of android devices
What i have tried so far...
Job scheduler
Work manager
Forground service
Background service
Alarm manager
push Notification
But all of these fails at some point on some devices
None of them is perfect
They stop working in sleep mode or doze mode and sometimes they do not work at all
Can any one suggest a tested and a bug free solution?
Related
I develop a running app that use GPS location through LocationManager to deliver a Turn by Turn navigation experience.
Yesterday, ï discovered that I do not receive any location updates when app in background (phone locked) anymore.
This could not be a code regression because none of my previous git commit work neither. And I know I could receive location updates during last tests few weeks ago.
Then I tested other GPS-based app like Waze and others, but same result : none of them could update my position when going in background (except for GMaps), meaning I have no navigation instruction if my phone is locked. I have tested with other phones Samsung S8, S9, and Asus ... no changes.
Does anybody know if there was a recent changes in LocationManager behavior?
Thank you.
I found a good workaround.
In order to receive GPS location while in background with Android 8.0, we need to create a foreground service that ask for location update and pass it to your activity.
Here is an example of that.
I am using service for sending current location of device to server continuously in background, but service is also getting killed when I swipe out the application on some phones. It's working if I remove restriction of running background services from Phone settings to the app. I will be thankful, If someone provide solution of removing this restriction programatically.
Every Phone manufacturer has it's own settings for background processing apps. As there are many android device manufacturers it will be difficult to check for every device group. I'm facing the same issue for my app. It will be better if you ask to check is there any setting regarding background services in their device.
I have an open source gps tracking application that has been around for many years. Recently, I have been getting complaints that in android nougat, instead of getting updates once a minute, people are getting updates from the phone once every five minutes when the phone is unplugged.
There is something going on with the power saver mode even though we tried turning it off.
Is there a way to force a phone to get gps updates at a specified time when unplugged?
Here is the code if anyone wants to see it but I don't think it's a problem with the code. It has been very stable for years.
https://github.com/nickfox/GpsTracker/blob/master/phoneClients/android/app/src/main/java/com/websmithing/gpstracker/LocationService.java
thanks.
One change in Android 7/Nougat was that the Doze is now "more aggressive". In Android 6/Marshmallow the doze mode kicked in when the screen was off, the device was running on battery and it was stationary.
(This is documented in Optimizing for Doze and App Standby)
Now in Android 7 the conditions are just screen off and running on battery.
(This is documented in Android 7.0 Behavior Changes)
Apps can be white-listed to be exempt of the restrictions if they break the core functionalility of it. In your case they do as the GPS tracker needs to record coordinates in real time.
There's a list of Acceptable Use Cases for Whitelisting
This includes:
Task automation app | App's core function is scheduling automated
actions, such as for instant messaging, voice calling, new photo
management, or location actions.
Users can white list an app manually on their own in the device settings or whitelisting can be requested by the app and approved or rejected by the user.
This is covered in Support for Other Use Cases
Quoting:
An app can fire the ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS intent
to take the user directly to the Battery Optimization, where they can
add the app.
An app holding the REQUEST_IGNORE_BATTERY_OPTIMIZATIONS permission can
trigger a system dialog to let the user add the app to the whitelist
directly, without going to settings. The app fires a
ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent to trigger the
dialog.
The user can manually remove apps from the whitelist as needed.
The last point is important of course, but luckily the whitelisting status can be checked programmatically:
An app can check whether it is currently on the exemption whitelist by
calling isIgnoringBatteryOptimizations().
So, dealing with the Doze mode is one thing to study at least.
Another issue is keeping the Service running in general. But the Service getting killed by the system might result in more random time intervals. There are of course the classic things like using START_STICKY or running as a foreground service.
I implemented getting location updates using FusedLocationProviderApi.
I need to track device location in background, when app is not running.
I followed the tutorials, I have a Service where I call requestLocationUpdates with an intent which starts a service.
I've purposely NOT implemented keeping a wake lock just to test and see that when the screen goes off, the device stops getting location updates. Once I verified that I could then go and implement the wake lock and expect the opposite, which is to see it working.
I used two devices: a Google Nexus 10 with KitKat and a Samsung Galaxy A5 (2016) with Lollipop.
On both devices, it keep sending updates (I'm sending the updates to a website).
I tested with screen off, after leaving device on a desk for a minute, and then walking again around the house.
I know Android 6 and Doze mode is more restrictive, but I want to nail and understand how it works on Android 4.4 and Android 5.
So frustrating! Any ideas how to actually make device go in sleep mode so I can see getting location updates stop working?
Funny, everything I've read is about making it work :)
EDIT: I'm confused why getting location updates is still working when screen is off even with an aggressive location update setup like the following:
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setFastestInterval(2);
locationRequest.setInterval(1000);
Thing about requesting location updates is that underneath it all, the LocationManagerService, which is what LocationManager uses, actually creates and acquires it's own wakelock. So technically, your device will never go to sleep as long as you have a location listener registered to actively receive GPS. Now, I know you are using Google's FusedLocationApi, but underneath it all, it does use LocationManager. If you are interested in understanding the location code a little better, here is the source code for LocationManagerService.
the last couple of days i was busy getting a service up and running for an phone gap application (using the eclipse ide with the device (motorola defy android 2.3.) connected to my computer)/ the service runs in the background when the application is off and has a timer scheduled that passes an intent to launch my application at irregular times/ everything is working as intended/ when the application is off and the device sleeps (screen is black) the timer is still running and at the scheduled time the device gets woken up, the lock removed and the app starts/ however, the bummer came when i unplugged my device from my computer/ the timer seems to work only reliable in sleepmode when the device is connected to my computer/ when its not connected to my computer the timer only fires correctly when the screen is on/ when it is not on it fires unpredictable at will and more often not at all/ i switched off the option that the device should not go in deep sleep when charging/ but it still works when the device is either connected to a power outlet or computer
can anyone try to explain what the reason might be?
is there anything i can do to make sure the timer is running as is should unplugged while the device is sleeping
Sorry my bad, it doesn't FULLY answer your question
One way is to register for ACTION_SCREEN_OFF and ACTION_SCREEN_ON event at the native level and propagate that event to phonegap layer (# html/js level).
More details on handling Screen Off and On intent click here.
More on how to Notify UI layer click here.