Android LocationListener stop receiving location updates - android

I have registered a location listener, which will receive location updates using NETWORK_PROVIDER. The listener receives location updates if the activity is in foreground. If I leave the app, after some time it stops receiving location updates. Which is a bit weird... isn't it supposed to receive location update even if none of the activity is on the stack? The listener starts receiving location updates again if I go back to my app.
Or am I doing something wrong? What programming model is suggested to receive location updates even if none of my activity is not active (neither on foreground, nor on stack)?
There are some other way to receive location updates (using pending intent), but those do not trigger if I do not turn on GPS.

The activity in the background will be eventually destroyed, you don't have any guarantee that once stopped it will continue receiving location updates. Activities in Android are not supposed to be used like this.
You can use a service with a wake lock as suggested, but keeping the device awake will quickly drain your battery.
I recommend you this talk by Reto Meier (IO 2011), where he talks about different strategies to have a fresh location. Here is the code.

Related

is it possible to get location updates in android app after force stopping(killing) the app

Is it possible to get location updates in android app after force stopping the app. In IOS it is possible to get the location update if we force stop the app, in similar manner is there any service which can provide location updates to killed app in android.
No, this isn't possible because the app's process has been killed and the registered locations listener has been removed. The system doesn't know who it should deliver the Location updates too. It the user force stops and app, by all means that means the app should stop what ever it's doing. But if the app is killed because of low memory pressures, then this is where using a STICKY service would come in handy. Simply register for location udpates inside of your Location Service and then inside of onStartCommmand return the START_STICKY constant to indicate to the system that if the service is killed (not force stopped), then the system should restart the service when memory pressure drops.
Yes, app could restart itself if you had some Geofence registered using BroadcastReceiver. Whenever device enter/leaves the Geofence app would receive the callback via BroadcastReceiver and in this callback you can reschedule location update listener.
But without Geofences location updates are not given to forced closed app.
Here you can know the basics about how to monitor geofences.

Android O: what will be effect of the new version of Android on Location Updates?

My app is currently using PendingIntent.getBroadcast to receive location updates from FusedLocationApi. How will my location updates be affected in Android O? I went through the documentation given in this link: https://developer.android.com/preview/features/background.html#broadcasts.
But I am still not sure about my situation. I have no foreground or background services to receive location updates from FusedLocationApi. I have registered a broadcast receiver which keeps listening to any updates from FusedLocationApi.
Thanks in advance.
Yes, your app is affected in several ways. You will receive updates only few times in a hour regardless how you setup the location request. In addition you can't start a service from your receiver, you can do it only if the service is in foreground or the user excluded your app from battery opmizations. So if you really need frequent updates, you need to start a foreground service in order to keep your app in foreground. The service can be "empty" (no work), you can keep your current design, the goal is just to keep your app in foreground

LocationClient seems to stop getting updates

I have an application which requires frequent updates to the user's location, and location changes are relevant from a number of Activities within the app. As such, I created a LocationManager class that manages the LocationClient object, listens for updates, and broadcasts the location changes so that any Activity can handle updates as they need to.
However, I'm running into a problem where the updates seem to stop when I change Activities. I noticed the following message(s) appearing when changing Activities:
09-26 11:19:14.854: D/LocationManagerService(735): Location requesting stopped by an application
09-26 11:19:14.854: I/LocationManagerService(735): remove gps (pid 1332), disabled
After that point, it seems that my LocationClient stops getting any further updates. I can call removeLocationUpdates() and then requestLocationUpdates() to restart the request, but my original request no longer functions.
My only idea is that because the main Activity uses a MapFragment (SupportMapFragment, to be precise) which displays the user's location, when I switch Activities that MapFragment likely makes a call to suspend location updates. But that shouldn't affect my requests.
Does anybody have any insight?

How to ensure location updates continue indefinitely?

The app is handling location updates and sending the location back to a server. The app does not turn the location updates off. This is for devices permanently plugged into power, so battery is not an issue.
By design, will the location updates continue indefinitely, or will Android stop sending them at some point, for example if the app is pushed out of RAM?
If the location updates do stop, how do I request them in such a way that they will continue indefinitely?
Yes, if phone is not asleep, then user processes will run.
Register a LocationListener in a Service so that it's triggered when location changes. Even if your service is not running, system will start it and execute the registered method.
You should definitelly read the Deep Dive into Location blog for all angles of location handling in Android.
It depends on how you are running the app. As long as the app maintains focus, unless something catastrophic happens, it will continue to send updates. If the app loses focus, then you might run into issues, depending on how you implement the location updating. Either way, be sure to
Create an ASyncTask[docs] to handle the actual sending of updates, otherwise your app will be killed
If the app can lose focus, you can create a Service, and detach it from your application to keep it running in the background.

How to stop GPS Updates in android when application is idle?

friends,
i have created an application in which i am getting GPS location onCreate Method and
onPause method i remove Location updates.
after taking gps update i left my mobile idle and whole night it was in idle mode and my application was running in it, in the morning bettery was down due to GPS i guess.
any one guide me how to handle this situation in which method should i stop using GPS if application is idle other than onPause()?
any help would be appreciated.
Stop the location update notification in onPause() is fine, but please note two things.
If GPS is still on, it will still eat your battery.
According to the activity life cycle if you start listening to location updates in onCreate() and stop it in onPause(), then when you return your application to foreground it will not register to updates again, consider moving the registration to onResume().
I asked a similar question about timing out the GPS if the user moves indoors where a signal cannot be found. I started a timer within the LocationListener class and remove updates if a signal isn't found within 5-8 seconds. There's some code here that might help How to time out GPS signal acquisition
If you stop receiving broadcasts (thus unregistering your BroadcastReceiver in the onPause() method of your activity), there is no reason why your application would keep receiving updates from the LocationManager.

Categories

Resources