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

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.

Related

Is polling gps location data within a non-wakeful Service a good practice?

In a regular activity, it is mandatory to register to LocationManager when we want to receive updates, and remove updates sometime later, e.g. within onPause() for instance.
However, in a non-wakeful service, which can die at any given time, how can I assure that the LocationManager is not keeping the GPS on?
(I have no guarantee that the onDestroy method of the service will actually get called, and even a foreground Service might get killed...)
I am asking this since I couldn't find any warning regarding polling location within a service, but I suspect that it might be a bad habit overall.
You can keep a service that can run with a interval. Service also contains a instance of your LocationManager and your current activity Context.
When your activity is destroyed your Context will be set to null and you can stop locationrequest and service by itself.
Experimenting with forcing stop of Services, it seems that the location manager
is smart enough to turn off the GPS whenever a LocationListener is dead,
so case is closed...

Android Location based services

I am a beginner. I want to develop an application which can generate location based notifications. In android developer website I found in order to maintain a balance between battery life and data exchange one should consider
frequency of new updates
window in which you listen for location updates.
I know frequency can be controlled by calling requestLocationUpdates(). My question is how can I control the window in which I listen for updates ? Does it mean that once I acquire the location update from onLocationChanged() method of LocationListener class, I should stop listening for updates using removeUpdates() ??
Thanks
GPS device only start working (and consuming power) when you register for requestLocationUpdates().
Teoretically the GPS can switch off between updates if they are not very frequent. Let's say for example that you have requested updates every 5 minutes, then the GPS can switch off for 4 minutes and 30 secounds aprox. and switch on in time to acquire the next location. If you set the new locations requests for every 5 secounds, GPS will not switch off between updates.
I have one application that keeps GPS awake full time, recording the locations in a database. This application can also show a map and draw the track recorded. My experience is that the power used to draw the map with track changing in real time is much higher then the power used by the GPS.
good luck
I think you should stop listenting for updates only when you don't need anymore new locations. If you need only one new location, stop listenting right away is the best approach. For getting more locations(as the user moves) it's recommended to start listen for location updates in onResume and stop listenting in onPause. That means your onLocationChanged will be called only when your activity is in front of all others (is visible).
In conclusion, the activity that manages the window you need should have these calls as specified above.

Android LocationListener stop receiving location updates

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.

Android: Timer in BackgroundService for Location

I have an app that consists of a few Activities. I want the when my app starts up, the I start listening for updates from the LocationManager, and after a few minutes, I want to time out and stop listening for updates. I've done this in other apps, but where I only have one Activity.
What I have now is a BackgroundService that starts listening for LocationUpdates; if it gets a good enough location quickly, it stops listening for updates. But sometimes it doesn't get a good location quickly, and I want it to time out, so I have a Timer that stops listening for updates after a bit. This works fine when I start the App and stay in the first Activity.
The problem is when I start the app, then start using the app and other Activities start, the Timer never goes off, so LocationManager is always on, until the app quits.
Thoughts? Is there a better way to listen to location updates so any Activity in the app can get the location, and ensure that it times out to stop listening for updates?
Update:
It turns out the BackgroundService gets destroyed when I switch Activities, and I cancel the timer in onDestroy(). Thanks for the response-- that was enough to confirm that the general approach was reasonable and I probably had a silly mistake somewhere (true).
Why don't you put the Timer in the background service ? That will solve the issue.

Android GPS Service Issue

I have a service (with a wakelock) that must run continuously behind the scenes capturing user Geo Location. The Service implements the LocationListener methods (i.e. onLocationChanged()).
However it takes some time for onLocationChanged() to get invoked by the phone, so in the meantime my service has to do something. I thought of using Thread.sleep(), but will that prevent the phone from invoking onLocationChanged()? Or should I do polling: while(i < 1000,000) {++i;}?
I'm not getting such abundant GPS results using either of those ideas; wondering if anybody can give me a tip on how to accomplish this.
I think you can use wait(), and notify() with synchronize block with LocationListener instance.
search for samples using wait(), notify().
I guess you want to keep the service "alive" while it is waiting for location changed information. That is taken care of by the system and you do not have to add code for that. When location change information becomes available, the onLocationChanged() would be invoked in the context of your service.
I solved the problem by removing WakeLock (waste of battery), doing a 10-second busy wait/Thread Sleep (just in case one blocks the GPS invocation of onLoc()), and using AlarmManager to wake the device from sleep and start the service.
This: gets GPS during sleep AND doesn't drain battery power.

Categories

Resources