How to survive background execution limit - android

There are some questions in my mind would like someone to answer and clarify:
I have background service that sends coordinates of location every second from background and updating location on another device like happens in driver customer model, what will happen to this app under background execution limit? And how will I survive this execution limit and after that what will be behaviour of my application? Will I have to start forground service for this?
Also what if I disable location permissions for this app frim settings while its working and sending coordinates, what will happen? Moreover can I notify user that location permission is disable and service is stoped?
Thanks

If you need frequent location updates (like for tracking exact path, the user was running) you definitely need a foreground service. Just add notification to your service and tell it to go foreground, displaying said notification:
startForeground(yourId, yourNotifcation)
If you need to know, when user happens to be in / outside some area, than Geofences are your best bet. Say, you want to know, whether he have left a train station. Or is nearby a certain shop.
If you need just infrequent (like every 15 - 20 minutes or more) updates of user location, than you can do this via WorkManager

Related

Gps updates in background for long time

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.

How to keep an app running continually with GPS on?

I have an application that is required to log the user's location every couple of seconds, the entire time it is on.
On does not mean in the foreground,or that even the Android's screen is on.
What is the suggested way to accomplish it?
I've heard of background and foreground services, and also saw something about Jobs. I also saw this was possible with a WakeLock.
I am not sure what is the best method of choice.
The need to conserve Battery life is of course an issue.
You should use Service, you can choose that the service wil keep running even after your app was closed.
YET if the device is low on resources it might close your service.
you can use FOREGROUND service if you want to minimize the chance android will close your service.
inside the service use LocationManager / FusedLocation to get the location every X time.
you set it up with LocationListener so everytime it set onLocationChanged you upload to firebase.
Useful links:
Make your app loaction aware
Services

Get location history in Android app?

I need to keep rough track of a users position, but not really in real time. It's sufficient to handle the location updates when the app is started. However, I still need to know where the user was when the app wasn't running.
Is there a way to get the location history in an app?
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
However, I still need to know where the user was when the app wasn't running.
That is not possible.
I don't really want to have a service just polling last known location all the time since that would be a waste of battery power.
Then eliminate your requirement for location history. You only get the locations that you request.
Rough Track can mean you get location of the user (lastKnown or Fresh) after every n-hours. Doing this will not require a service, simply a recurring alarm and receiver will do. In the onReceive method of receiver, you can manage a stack of locations in your app.
You will have to reset the alarm though when the device re-boots. I guess this is an add-on, rest should work fine.
you can not get current location without running your app...
the second way is to make background service ..wich you don't wan't to make...
The other way is to run background service using Alarammanager whenever you want after getting location you can stop the service...like you can make call every hour or 2 times per day...

How to put GPS system service in a local service so it can be killed to turn off GPS

Like many others, I want to have an exit button that will turn off the GPS when the user leaves the app to conserve battery life. Many discussions are posted basically saying this cannot be done but I have an application that does it so continue to try and find a way. The developer of that application told me that he runs the internal GPS in a service that he wrote and that his exit key kills that service and vola, the GPS indicator goes off instantly.
My app is very time sensitive and I would like to use the equivalent of onLocationChanged that I use now when using the LocationListener part of the system service but have the system service inside a service I write so that I can kill it.
My question then is can I put the system GPS service inside a local service without adding any significant delay, without me having to poll the local service for updates. Any help on how I can do this would be appreciates as this is my first Android app and although I have written tons of C and PHP code, this is new territory and a bit strange.
Like many others, I want to have an exit button that will turn off the GPS
You cannot "turn off the GPS". You can tell Android that your app no longer needs GPS (e.g., you no longer need GPS updates). Android will then determine whether or not to power down the GPS radio, depending on what else might be trying to use GPS at the moment.
when the user leaves the app to conserve battery life.
You do not need a button for this. Simple apps, where only one activity needs GPS access, can simply request location updates in onResume() and remove them in onPause(). More complex apps might request location updates on the first onResume() that needs them, then remove those updates in onUserLeaveHint(), or do a reference-count of resume/pause operations to determine when to remove updates.
There may be scenarios where you really do want the user to have to explicitly say "stop using GPS" by clicking a button, but if you can avoid it, please do so.
The developer of that application told me that he runs the internal GPS in a service that he wrote and that his exit key kills that service and vola, the GPS indicator goes off instantly.
Somehow the developer needs to determine when to start and stop this service. Rather than starting and stopping the service, they could request and remove the location updates, and have the same effect, while consuming less heap space and making it less likely that the user will attack you with task killers and the Force Stop button.
IMHO, the only reason to use a service with location updates is because you specifically want to consume location information in the background with no activities around.

Android background service and AlarmManager

I am coding an android application that gets the user position every 5 minutes, stores it in the database and sends it to a server.
I have read many ways of doing it, I was going to do the following:
User starts the application
The main UI activity starts a service.
The service runs in background and keeps turning on and off the gps, and creating new
threads that will save to database,and send the data to the server.
But I have seen that it can be done with a "Remote service" (http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/RemoteService.html) or with an AlarmManager that schedules starting this service every 5 minutes.
The service will need to be running always: it is important that after every interval (5 minutes), it is executed.
I think I need some clarity here.
Thank you for your help,
I am coding an android application that gets the user position every 5 minutes, stores it in the database and sends it to a server.
Please allow the user to choose the location provider. Not everybody has GPS on their device. Not everybody has GPS enabled. And, not everybody will want the power hit of GPS being turned on every five minutes.
Please allow the user to choose the polling period, including "never poll -- I'll refresh the information manually from the activity". Also, please honor the background data setting (deprecated as of ICS).
I think I need some clarity here.
If the polling is supposed to go on even if the activity is not in the foreground, use AlarmManager. However, most recipes for using AlarmManager will have the real work (in your case, GPS fix and network I/O) be handled by an IntentService. This will not work in your case, because GPS is asynchronous -- you cannot just get a fix whenever you feel like it. It will take a long time, possibly forever, to get a fix, so you have to deal with the delay and eventually timing out the operation. Writing a Service to do this is possible, but tricky, particularly if you are aiming to collect this information even if the device falls asleep.
If, however, the polling is only supposed to go on while the activity is in the foreground and the device is on, I wouldn't bother with a Service at all. Just have the activity use postDelayed() to set up an every-five-minutes scheduled bit of code to run, then have it do the work.

Categories

Resources