Android - GPS after a while no longer receives coordinates - android

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.

Related

Android Background Location Updates

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.

Android Location GPS Track

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.

How to check if your location has been confirmed in Android?

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.

Request GSM/UMTS Location Update in Android

Let me summarize my problem and what I would like to achieve.
I have a SonyEricsson X10i phone with Android 2.3.3. I realized that sometimes my phone not receiving calls even if it indicating full coverage. I checked myself in the MSC/VLR and it indicates that I registered and my phone is currently active (and also there is no IMSI DETACH flag), so it should working correctly (only the last Activate Date is a little bit old ~couple of hours, which can be good as well, without SMS/Call/Location Update), as I mentioned before the phone indicates full coverage and it seems it’s on the network. But when I tried to call it I only reached the Voice Mail.
In the MSC/VLR I see No Paging Response Cause for the call, but the phone does nothing. I tried with other SW version (4.0.3 ICS), but the same result. But I not noticed similar behaviour with a different handset (same type).
Sorry for the long summary.
So because what I described above, I ‘m trying to write an application/service which will perform GSM/UMTS location update in 15-20 minutes, but I couldn’t find any kind of procedure in android.telephony.gsm.GsmCellLocation, android.telephony.TelephonyManager which will do this for me.
My other concern is the
getState()/setStateOutOfService()/ setState() procedures from ServiceState class…
It seems they not really working. For example, when I first call the getState() I always get back STATE_OUT_OF_SERVICE, which is not true…
When I’m set the state to STATE_POWER_OFF or STATE_IN_SERVICE, at least I get back that state from getState() afterwards, but the phone does nothing for that . (Not even indicate out of coverage,etc…)
Every suggestion/comment are welcome.
I have also seen this problem many times (2 phones from the same manufacturer as yours). From your question, I understand that you want to force the phone to send an MM periodic location update (which it should be sending anyway).
This is too low level, and there's nowhere you can force this directly in the programming interface. The mobility management procedure is part of the phone stack, and is specified in detail in 3GPP TS 24.008, available from www.3gpp.org. Paragraph 4.2.2 defines when the phone is supposed to send these location updates.
The only other thing would be to try by indirect means to force the phone into a condition where it would send a location update. You might be able to do that by trying to select another network manually. If it's successful, and you then manually re-select your home network, then you would trigger a location update. If it's rejected and falls back to its home network, then I think a location update would be triggered as well.
But there would also be small costs to this - battery use while it does a networks scan, and time lost while it scans and does manual network selection.
(My personal experience is that the lost calls don't happen often enough to justify this.)

Detect when android device is in a moving car

For a personal project, I'm trying to detect when an android device is with someone driving. I initially thought of using GPS to determine average velocity, but decided that would consume power and requires that the user has GPS enabled at all times. I'd heard that it's possible to tell when someone switches cell towers (on both android and iOS), but is there a broadcast intent for this? I'm very new to Android development, so was hoping I could register a broadcast receiver to run when I believe the device is in a moving car.
I'm not sure if I can create my own broadcast intents and I assume I couldn't rely on a background process to always be running since android may kill it if memory is needed.
As a side note, I notice that some apps, like skype, have a background process that restarts itself even if I force kill it. How do they do this? My thought would be to have a broadcast receiver for when device moves, turns on, etc. to restart the background process... is that what they do?
Thanks for any info!
I would suggest GPS would give you much better indication of motion, since you likely change towers somewhat infrequently. Even mapping applications that are able to triangulate your position using tower signal are extremely inaccurate. Often even with a very good signal they aren't able to triangulate your position any better than within 1600m.
You could also look into asking the user if they have a bluetooth kit in their car. If they have you can find the name of this pairing and when connected you know they are in the car (or close to it).

Categories

Resources