I have created an application that has a 'geolocation' feature responsible for spotting a user on the Google map like many other applicatons. I used "LocationManager.NETWORK_PROVIDER" to locate the user and at the same time I instantiate and start "MyLocationOverlay" (in the onLocationChanged() method) to get the location. Because of the second one, the GPS turns on (blinking on the top) which is OK.
The problem is, after the application is closed (back button or through task manager), the GPS feature is still hanging there, trying to get the updates.
How to turn it off after the user leaves the activity? I tried suggestions from here and other forums like putting locationManager.removeUpdates(this); and locationManager.removeUpdates(mMyLocationOverlay); within the methods onPause(), onStop(), onDestroy(). The method OnPause looks like this:
#Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
locationManager.removeUpdates(mMyLocationOverlay);
}
('this' references my class that implements LocationListener)
Please, can someone help me to turn off GPS updates after leaving the activity (it's a class that extends MapActivity) but not turn off the GPS feature on the phone itself?
Interesting thing is that when I remove the part with MyLocationOverlay, GPS will not start of course and therefore no problem. So I'm pretty sure that mMyLocationOverlay is the listener that "won't stop" and producing a problem.
googleMap.setMyLocationEnabled(false); in onPause() solved my issue. And I'm setting that to true in OnCreate(). Hope this may help others.
If you want to close (or end) the application you can use
System.exit(0);
so when the application is closed, all the services you use will close.
Set your LocationListener equal to null and re-instantiate onResume()
Related
So, I have an app with a splash screen that starts another activity once it's done loading. When the user hits the home button while it's loading, the application goes to the background and then comes back to the foreground once that activity is started. Is there any way to stop that from happening?
Two things:
First, make sure you're tearing down the reference to the location service in onPause. I assume you're using Google's API Client. If you're not, you really should be. So in onPause, make sure you unregister the listeners:
#Override
public void onPause()
{
// Tear down Google API Client.
if (googleApiClient != null)
{
if (googleApiClient.isConnected())
{
// Turn off location polling.
LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient, this);
}
// Avoid leaks by making sure callbacks get unregistered.
googleApiClient.unregisterConnectionCallbacks(this);
googleApiClient.unregisterConnectionFailedListener(this);
googleApiClient.disconnect();
}
}
BUT: I think you're over thinking the solution on this one. Why have two activities? Why not one, and have a "wait state" until you get the location fix? Your wait state could be anything. Or a full screen splash as you say (Use a RelativeLayout and stack the views). When you get the fix, fade out the splash.
Then stash the location to the savedInstanceState bundle. When your activity's state changes, you'll know to not display the splash again.
Here's the case: my app calls onPause: theLocationManager.removeListener(..).
In android 4.4.2 the gps icon at top bar disappears and after some minutes it appears again (app still in background).
Icon will disappears if I remove the app from the recent list.
Android 4.1.2 does not has this problem.
Any ideas of how to disable gps when app goes background? Is there a workaround for 4.4.2?
Thanks!
I believe this is may be to do with the way the destroying of activities has changed (not entirely sure though). I believe you want to do this in the onStop() method, so that when this activity is no longer visible, updates stop being requested.
#Override
public void onStop() {
super.onStop();
locationManager.removeUpdates(this); // Assuming the calling class implements a location listener
}
This should help, worth a try anyway.
My application is essentially a gps tracker. It binds a service to the GPSActivity which creates a thread to calculate distance, speed and things like that.
Before I had the gps thread in the service it was in the GPSActivity and worked fine. I'm switched it to a service so it would be able to persist in the background if the user needed to use another application.
However sometimes at the beginning of the gps tracking peroid the onKeyDown method in GPSActivity is being called with out me actually pressing the button.
Does this mean that onDestroy is trying to be called and that I'm using too much memory?
Any help to explain this behavior is greatly appreciated.
I solved my problem by adjusting my onKeyDown method as follows:
if(keyCode!=KeyEvent.KEYCODE_BACK){
return super.onKeyDown(keyCode, event);
}else{
//must be back button
I am currently working on a task where i need to fetch the locations from GPS.
The concern i am having here is that once i obtain the location & the task goes completed, after this if the application goes in background i do not wish to obtain any locations until & unless my application is back on screen (foreground) whatis currently not happening. (kindly note here i am not talking about switching the activity from one to another via intent as my app has only one screen & one activity).
I wish to know what place exactly the code to handle this situation must go & how can i do achieve this. I have tried with setting the instance to null & remove update but it proves ineffective. May be i am putting it at wrong place i am putiing it outside onCreate() inside the class extending activity.
Call requestForLocationUpdates() in onResume()
and removeUpdates() in onPause()
Once I obtain the location & the task goes completed
If you just want one Lat, Lon fix: don't use the tracking mode, instead use : requestSingleUpdate()
Original:
Is there a way to kill a child activity of a TabActivity. I have a very simple tab setup. There are two tabs. One of the tabs contains a MapActivity. When I switch to the MapActivity the GPS turns on. When I press back, the TabActivity exits, but the GPS is still on. The gps doesn't turn off until I explicitly kill the app. Is there a way to kill the MapActivity from the TabActivity? I would like to kill it when TabActivity exits/onBackPressed.
Edit:
I do attempt to stop location requests in onDestroy and onPause in my MapActivity. I am using the MyLocationOverlay class an calling disableCompass and disableMyLocation in the onDestroy method. These methods are called, but the GPS remains on. If I call enableMyLocation and immediately call disableMyLocation (during onCreate), the gps will turn off.
Edited title to reflect the answer:
Turns out the problem is unexpected behavior with MyLocationOverlay's enable*/disable*. These functions do not appear to be idempotent. Multiple calls to enable and disable do not turn it on once or off once as one might expect. Ensuring one call to enable (in onResume) and one call to disable (in onPause) will disable location requests when exiting an activity.
The location service cannot be killed by stopping an activity, you should read up more on location services and how they operate:
http://android-developers.blogspot.com/2011/06/deep-dive-into-location.html
Specifically, when you request location updates from the providers (in your case the GPS provider) you should only request it for a certain period of time, and then you need to make a call to stop the requests.
Also see:
http://developer.android.com/reference/android/location/LocationManager.html
The best thing to do would be to override onStop() or onBackPressed() to stop the GPS. When you are in one of the activities, you have the option of calling getParent() to return the tab activity, as a side note.
It turns out that calling MyLocationOverlay.enable*() multiple times (I was calling it twice) makes the GPS stay on even if you call MyLocationOverlay.disable*() as many times. At least, this is the behavior I observed. I expected enable and disable to be idempotent, but they are not.
Related problem