Android MapView keeps Location on - android

I have a simple fragment with a mapview displayed. I have override all the necessary methods according to the documentation (destroy, resume and start) to cal the same method of the mapview. However when I detach the fragment or exit the application the location keeps on. It will drain too much battery. Is there any procedure I can make to stop the location feature?. I've tried calling mapview.onDestroy, mapview=null, etc etc and the location keeps on.
Thanks

Check How to unregister Listener & stop service from within broadcastreceiver. If you're using LocationManager to get location updates, call removeUpdates(getActivity()) on your LocationManager instance to stop them.
Edit as requested:
Searching the current location via GPS is disabled by calling setMyLocationEnabled(false) on the GoogleMap instance.

Related

Can not stop fusedLocation updates using removeLocationUpdates

I have created a class naming MyLocationProvider with all methods provided in fusedLocationAPI as well as the following to stop the location updates:
private void stopLocationUpdates() {
mFusedLocationClient.removeLocationUpdates(mLocationCallback);
}
In main activity I use this class like this:
MyLocationProvider mylocation=new MyLocationProvider(this);
Location updates works fine but I am confused how requestLocationUpdates works. When I close then application, requestLocationUpdates is not stopped (it seem something like background process but is not listed in device services). When I open the application, a new instance of MyLocationProvider also starts working and I receive multiple parallel updates from my device.
How should I use removeLocationUpdates to stop an exact instance of location provider when they seems not to be a part of my application?
Call this method on stop or on destroy method of fragment or activity which is used by u.

Android: get location updates without interfering with osmdroid.MyLocationOverlay

Android / location:
I would like to receive location updates every 5 seconds.
Should I call the method:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 0, gpsLocListener)?
It is important for me NOT to interfere with the osmdroid MyLocationManager functionality.
Or should I use the MyLocationOverlay.onLocationChanged(), e.g. by inheritance of the MyLocationOverlay class.
If so, how often will it be called?
There should be no problem having multiple listeners on the location manager. Each one will get events separately. In fact, if you wanted you could use a new instance of osmdroid's GpsMyLocationProvider to feed you location updates. Call mGpsMyLocationProvider.startLocationProvider(myLocationConsumer) where myLocationConsumer is an instance of IMyLocationConsumer that receives and handles gps updates as you wish. You can call mGpsMyLocationProvider.setLocationUpdateMinTime() to adjust how often you get updates.

The GPS updates won't turn off after closing application

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()

MyLocationOverlay won't disable location requests

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

doubt in using Location Listener

I have used LastKnownLoaction at the start-up Activity of my app to get the location ,if it returns null then i register for a Location Listener for both gps and network providers.After requesting for Listener to listen for any location change i switch over to main Activity page using startActivity.Further in the startup Actyivity's onLocation in both Listener i am trying to get location and if any values found then i try to store them in preference and remove updated from both listener and notify the user about the location being updated.
Is this logic is correct way of coding ,do the listener listen for location update when its no longer on the top of the stack ,hence i am using a emulator i couldn't test gps functionality,i required your suggestions on this
thanks
You shouldn't register a LocationListener only if getLastKnownLocation() returns null, because that means the particular location provider is disabled. Also caching a real-time value like location doesn't sound like a good design. I would suggest to register/unregister LocationListener for each Activity in it's onStart/onStop methods.

Categories

Resources