Stop Location Updates after first time - android

I am using a service which implements LocationListener class.Now i want to know the current location(Latitude,Longitude) of the device. I have used requestLocationUpdates method so that onLocationChanged method gets called when the location changes. I want that onLocationChanged method should be called only once i.e. when i get the location for the first time, it should not be called again.
I have called stopSelf method to stop the service inside onLocationChanged method. But still i am getting location updates.
Please help me.

Call removeUpdates on LocationManager, passing your location listener.
mLocationManager.removeUpdates(mListener);

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.

requesting updates using location listener in service is stopping another location listener in activity

When I requestUpdates in the service using location listener, another listener which I have put in the activity doesn't work. The moment I stop that service (from within the app using toggle button), the activity starts listening for location again. I want both working. Why both are not working simultaneously? Am I missing something?
Try adding this to your activity :
#Override
public void onDestroy() {
super.onDestroy();
getActivity().unregisterReceiver(receiver);
}
I figured out how the requestLocationUpdates( ) function behaves.
CASE 1: [my case]
I was using two separate Inner Class implementations of LocationListener, one in the Activity and one in the Service, each requesting for location updates at the same time via requestLocationUpdates(.., .., ..,locationListener) function.
These locationListener objects were not instances of SAME class, so the one who first got used in requestLocationUpdates() function, only it was working.
CASE 2:
On the second hand, if you call requestLocationUpdates() with instance(s) of SAME LocationListener class, all of these work simultaneously.
CASE 3:
On the third hand :D, if you call requestLocationUpdates() multiple times with one same instance of a LocationListener implementation, the most recent call takes over the previous ones with whatever minTime and minDistance passed in the function.
Note: all of above cases are tested on the requestLocationUpdates(String provider, int minTime, int minDist, LocationListener listener) version of function.

Android MapView keeps Location on

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.

How to stop a service from a service

I am using location listerner in a service, and i have overriden onLocationChanged(Location) method. Now i want that whenever this method is called for the first time, i want to stop the service. So please tell me how to do it
Call stopSelf from the onLocationChanged method (assuming the Service itself implements that interface).

Triggering LocationListener's event manually

Is there a way to manually trigger the code inside a LocationListener's onLocationChanged method? It should receive a Location object with the last known location, I guess.
Sure thing, if it's your LocationListener - onLocationChanged() is a public method; just call it with whatever Location object you have on hand (maybe have your location listener cache it somewhere, or use LocationManager's getLastKnownLocation method to grab the last location seen from a specific provider and call onLocationChanged() with that).
You can use the setTestProviderLocation method of the LocationManager to mock new locations and have the onLocationChanged method of the registered listeners called when you want.
You should check the reference page. You also have an example here.

Categories

Resources