I'm building an app that involves getting the user's current location. I already have it getting the location, but sometimes it's inaccurate.
I've noticed that when I start the app, a location services symbol appears greyed out in the notice bar. While it's greyed out my app will sometimes report that I'm where I was an hour ago, even if I'm miles away. A few moments later the symbol will be solid and then the app reports the correct location.
The phone seems to understand that it's not entirely sure for moment. Is there a way to check this programmatically?
I'm aware of how to check if location services are enabled. The problem is that my location is not updating quickly enough and I'd like to wait until the phone is absolutely sure of where I am.
My current fix is to make a fake loading screen that just forces the user to sit for a few seconds, but this seems a bit sloppy. Also, I have no guarantee that the location will have updated properly by the end of the three seconds.
I'm trying to avoid making the app run in the background and check periodically because I'm worried about draining the battery.
Related
How does one monitor location in the background on Android these days?
We were using a foreground service to do this but with the changes in Android 12 and 13 this does not seem like the right way to achieve continuous location updates and seems like we are getting crashes. Fatal Exception: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false:
Our use case is:
app integrates with a bluetooth speaker and when a button is pressed on the speaker an event is triggered in the app. The app calculates a distance and sends that back to the speaker to be announced. We are calculating the position between the phone and another known location (LatLng). So we need to keep track of the users current position in order to calculate the distance at any point in time. A user could be doing this for a couple hours without opening their phone.
I have been trying to find good resources on how to monitor location in the background but is a bit tricky to find any resources that take into account the recent changes from the OS and newer Android APIs.
It seems like WorkManager would be an appropriate solution but I am not sure about hitting work restrictions when trying to monitor location for an extended time.
Any and all help / resources appreciated.
I have a small test App that with an Android GPS API map fragment. I use FusedLocationProvider. TarketSDK=29. Using Java.
As long as the app is active it works beautifully. On locationUpdates, I add a new point to the track and everything looks great and stays accurate. The goal is to track my hike, total distance and track and show it on the map. Works great.
As soon I lock my phone or loses focus, then the updates stop and I no longer get location updates.
Solution seems to be:
Background Service (discouraged)
Foreground Service
PendingIntent
I have poured over the docs, StackOverflow, all examples/tutorials I can find, developer.android.com, etc. I have downloaded examples of the latter 2 from GitHub; they seem incredibly obtuse (probably just me).
What are the dis/advantages of ForegroundService vs PendingIntent?
How about a bare-bones example illustrating the min features of each to implement location updates while your phone is locked in your pocket or some other app is active? Just the template minimum.
I need to save the locationUpdates that occur while my app is not active or phone is locked; in order to fill in Track when activity is restored to the app.
Some simple end-to-end guidance from my working app to something that will maintain locationUpdates and save the data would be great.
Ok - I have answered my question in a roundabout way.
I had been Searching on "retrieving location updates when app is not active". This lead to the various solutions of background service, foreground service, pendingIntents, etc.
I eventually found that if you just start a Foreground Service with a Notification, even if your phone is locked or you switch active apps, your App continues to receive LocationUpdates; as the Foreground Service runs in the same thread and therefore activates your app code (if I understand the reasons why correctly).
So, I started searching on just how to start a Foreground Service. As anyone knows that has tried to figure this out lately, this has changed more than a couple times over recent versions. The online docs at developer.android.com are not up to date. You will spend a lot of time wondering why things do not work following these docs.
Eventually, with just searching on how to start a foreground service, I came across this simple and straightforward (non-youtube-video - don't you just hate those things) tutorial. https://androidwave.com/foreground-service-android-example/
I just added this code to my existing Mapping code that works when the app is active, and tested with locking the phone and putting it in my pocket and switching apps and doing the same. It appears to solve the problem.
Update: Added code to count number of location updates and average accuracy of each update holding the phone in hand, screen on and app active as the baseline. Phone locked, or App not active no difference in number of updates nor accuracy. Phone locked and in pocket, no difference in number of updates, but accuracy suffered by from an average of 10m to an average of 13m; to be expected I assume whilst in the pocket.
I made an Android app that makes lots of stuff.
One of them is GPS tracking, saving marks to draw a trail.
Sometimes, Android closes my app when the phone is out of my sight in my pocket (I guess it's Android's "smart" memory management...??), and thus its GPS tracking stops.
I have used some tracking apps that keep running for long periods, so I guess they do something that I don't know.
How can I prevent this sudden stop? Can I add some kind of priority to the GPS tracking to prevent this, or some other way?
EDIT
Observe that I don't want just to get tracking even if app is closed. I want to prevent Android from closing the app, since the GPS tracking needs to be logged by the app.
I think what you are looking for is to receive location updates even if after closing app, This tutorial will help.
Create ForegroundService. System will not drop it until you close it by yourself.
More about in Documentation
I have a big problem with an Android application that I developed.
The purpose of the application (for business) is to track the position of the device continuously (24 hours on 24) detecting a GPS track on a regular interval, which will then be synchronized to the server to the unleashing of certain events.
Of course, over time the device in and out of buildings, acquiring and losing the GPS signal continuously.
Often the device is not used and remains in office for several hours inside the company headquarters without GPS signal. During the course of time the Android system continues to provide me constantly getLastKnowLocation
My problem is that after some time that the system is running, sometimes two or three days, sometimes more days, my app starts to receive from the system always the same coordinates, regardless of who is in the open or at closed. From what I understand the Android operating system no longer seems able to update his coordinates and It will always return the same getLastKnowLocation indefinitely.
My App therefore becomes useless.
You know how I can fix this?
Is there any process that Android can restart in order for the system to wake up. In My App I will acquire any permission, except for root permissions.
For now the only control that I put, and that if the system always gives me the same identity for a number of seconds I call the method requestLocationUpdates of the location manager again.
But I need something more robust, to give me a better guarantee of operation. I'd like to be sure as possible that the systems try really to get updated coordinates.
The only thing I can think of is that maybe I can ask the system to restart some process so that the Location Manager, is fully reset. Can I do this? What is the process to be killed?
Can you think of any other solution or you found yourselves in this issue?
Thank you.
There seems to be a problem with the communication between GPS and my android application.
About 1 in 10 times (after starting the application) the GPS isn't triggered by LocationManager.requestUpdates() (no GPS icon shows up etc.).
I've built in a timer that checks the GPS status every 15 seconds and when the GPS isn't working it'll remove the updates from the LocationManager and requests the updates again (the GPS status is checked through GpsStatus.Listener). Sometimes this wakes up the GPS and the problem is solved, but most of the time this doesn't work. However, turning off GPS on the device and turning it back on [sometimes] fixes this as well.
The application has lots of users with different devices who have this problem. I have a foreground service running at all times which requests these location updates in onStartCommand() and it removes the updates in onDestroy(). I've tried a lot of different solutions, but none of them seem to work (frustrating). I tried the fused location provider, but this doesn't always give the most accurate location/has no bearing/speed etc. Is there any other way to give the GPS a poke to wake it up or has anyone had this problem before? Also, I noticed that whenever this happens and I close the application, remove it from memory, and start it up again, the problem is fixed as well. It looks like something in the application is blocking it from communicating with the GPS (no idea if this is possible).
I'm curious if anyone has any suggestions as to what the problem could be.
Thanks in advance,
Wow, so I've been frustrated with this issue for a couple of weeks and apparently it's because of Flurry. Flurry reports GPS locations as well and this conflicts with the GPS of my app.. Calling FlurryAgent.setReportLocation(false) fixed this issue for me.