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
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 want to track user's real world activity on android ,which method is better ,GPS or activityRecognition?
and one of the situation is when user suddenly running, i want to detect user's activity change immediately how can i do?
According to your basic requirements I'll suggest you to use ActivityRecogniserAPI, but ActivityRecogniserAPI doesn't give you the results immediately specially on the places where Location Services are not responding quickly.
But if you use GPS then you can speedup the process little bit, but you have to do a-lot or work to achieve this by running some process, doing calculations, handling events etc. It will be little bit fast as compared to ActivityRecogniserAPI but you will be having issue in this as well when Location Services are not responding quickly.
If the slow process works for you then I'll suggest you to use ActivityRecogniserAPI.
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.
I am developing an Android application that has behaviour that should not be interrupted by anything. Basically, if it's running and doing what it's supposed to do, nothing else should be happening on the device.
In order to achieve this I don't want to rely on user in terms of disabling data sync, incoming call ring etc. - and app updates.
It is important for me because even on quad core CPU app updates can cause rest of the system to become slow and unresponsive for short periods of time.
I know that there's an option to disable automatic updates in Google play settings, but I want to do this programmatically. Is there any API call that allows to do this?