Unsubscribing a LocationListener from the LocationManager - android

How do I unsubscribe a LocationListener from recieving updates from the LocationManager?
Here is how I'm setting it up
mLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
mListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener", "Logging Change");
}
}
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 1, mListener);
After I have exited the the view that created the LocationListener I am still getting log messages in the LogCat window.
I understand that this is because I am orphaning the listener but I cannot see any destroy method on the LocationListener nor can I see any "remove listener" style methods on the LocationManager object.

Call removeUpdates on LocationManager, passing your location listener.

mLocationManager.removeUpdates(mListener);

I think removeUpdates should help.
mLocationManager.removeUpdates(mListener)

Related

How use LocationManager inside Timer?

in my app I have a Background server and inside server class I have timer and inside timer I have a locationManager to find location :
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 10,
locationListener);
but here I got this error :
java.lang.runtimeexception can't create handler inside thread that has not called looper.prepare();
my question here how to use locationManager inside timer ?
Look Background service different with Activity when you use runOnUiThread this is just possible with activity be careful OR if you use thread inside Service class you can do this like this :
inside service class write this :
static Activity ac;
public static void setActivity(Activity a) {
ac = a;
}
and in your activity class do this :
MyService.setActivity(this);
now this thread just like this :
(ac).runOnUiThread(new Runnable() {
#Override
public void run() {});
I strongly recommend it to you do like what I did .
The exception message may seem arcane if you don't know what Looper threads are, but it basically means exactly what it says: you can't create a Handler (or any object that contains a Handler) in a thread that is not a Looper. Timer threads are not Loopers. Many things in Android are intended to be created only on the UI thread, which is a Looper.
Trying to use a LocationListener "inside" a Timer doesn't make much sense, anyway. The LocationListener's methods are called by the system, not by your code. I suggest searching and reading up on event-driven programming.
((Activity) getBaseContext()).runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 10,
locationListener);
}
});

do following task after onLocationChanged is called()

I have a class implementing the LocationListener
public class GetLocation implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Log.i("GetLocation", "Location changed: " + location.getLatitude()
+ ", " + location.getLongitude());
}
In my activity,
final LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
final GetLocation locationListener = new GetLocation();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
..... //Check whether the location is changed
locationManager.removeUpdates(locationListener);
updateGPStoServer();
After onLocationChanged() is called once, I want to do a upload task and cancel the listener, so I am asking how can I wait for the onLocationChanged() then do my tasks?
After onLocationChanged() is called once, I want to do a upload task
and cancel the listener, so I am asking how can I wait for the
onLocationChanged() then do my tasks?
I think you need to move the call to updateGPStoServer() into your Listener.onLocationChanged method; that would implement the "waiting" that you're looking for. (On a separate note: updateGPStoServer() should be implemented to create a background Thread to do the updates to the server. But you knew that, right? :-)
Also, it sounds like you really want to be calling LocationManager.requestSingleUpdate instead of LocationManager.requestLocationUpdates. That would remove the need to call locationManager.removeUpdates(locationListener).

LocationManager providerDisabled()

I want to know if a Certain Provider is Disabled will the LocationManager stop listening to the location changes for that provider ? If not then how can i manually stop listening for updates for that provider. Thanks In Advance
Your location listeners will not stop until you call myLocationManager.removeUpdates(myListener);. Check my answer to a different question [here] to know what else you need to do for connection status updates1.
give this a whirl
myLocationManager = (LocationManager)this.getSystemService(LOCATION_SERVICE);
myListener = new LocationListener() {
public void onLocationChanged(Location location) {
Log.i("LocationListener", "Logging Change");
}
}
myLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
5000, 1, myListener);
myLocationManager.removeUpdates(myListener);

How to get constant GPS location

I have following code:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
and MyLocationListener class:
public class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
tv_GPSlat.setText("Latitude: " + loc.getLatitude());
tv_GPSlon.setText("Longitude: " + loc.getLongitude());
}
#Override
public void onProviderDisabled(String provider){
Toast.makeText( getApplicationContext(),"GPS is not working", Toast.LENGTH_SHORT ).show();
}
#Override
public void onProviderEnabled(String provider){
Toast.makeText( getApplicationContext(),"GPS is working",Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}
I would like to save current longitude and latitude to my TextViews (tv_GPSlat, tv_GPSlon) but the location values are not constant (they are changing all the time). How can I do this?
GPS isn't exact- even if you don't move it will bounce around a bit. Just put up the first location you get, and ignore future updates unless they move by more than a certain amount. That's the easiest way to do it.
You have to get the location and once you get it (i.e. your handler method is invoked) you have to unregister the handler in order to stop receiving the updates. Simply add this line at the end of your handler method onLocationChanged() in MyLocationListener:
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.removeUpdates(this);
Add a data member to your location listener, and keep the previous location in it:
public class MyLocationListener implements LocationListener {
public Location mSavedLocation;
#Override
public void onLocationChanged(Location loc) {
// If we don't have saved location, or the distance between
// the saved location and the new location is bigger than
// 5 meters (~15ft) save the new location
if ((mSavedLocation == null) ||
(loc.distanceTo(mSavedLocation) > 5)) {
mSavedLocation = loc;
}
// Update the screen with the current saved location
tv_GPSlat.setText("Latitude: " + mSavedLocation.getLatitude());
tv_GPSlon.setText("Longitude: " + mSavedLocation.getLongitude());
}
// ... no changes to the rest of the class
}
Now the rest of your code can also get the latest saved location using:
mlocListener.mSavedLocation
I wonder why no one mentioned this. May be I a missing something. The call you have made has 0,0. It should have milliseconds, distanceinmeters. This way location change is only called when a particular distance is traveled OR after a time out. I am using both GPS and NETWORK providers to not be dependent on either too (sometimes GPS is not reliable).
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, God.KM20TIME,
God.KM20DISTANCE, (LocationListener) updates);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, God.KM20TIME,
God.KM20DISTANCE, (LocationListener) updates);

Capturing system event in an Activity

I'm planning to have a button in an activity which will start a service when clicked. However, GPS needs to be enabled for the service to do its work so I'd like to have the button disabled if GPS is disabled. Is there a way to get android to notify my activity when GPS is enabled/disabled so that I can enable/disable the button accordingly?
This link describes how to create a location listener:
http://blog.doityourselfandroid.com/2010/12/25/understanding-locationlistener-android/
I've copied the important parts down below in case the site goes down in the future. The first step is to create a LocationListener:
private final class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location locFromGps) {
// called when the listener is notified with a location update from the GPS
}
#Override
public void onProviderDisabled(String provider) {
// called when the GPS provider is turned off (user turning off the GPS on the phone)
// Dim the button here!
}
#Override
public void onProviderEnabled(String provider) {
// called when the GPS provider is turned on (user turning on the GPS on the phone)
// Brighten the button here!
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// called when the status of the GPS provider changes
}
}
Then you'll want to register that listener. This should probably go in your onCreate()
LocationListener locationListener = new MyLocationListener();
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 35000, 10, this.locationListener);
The second and third parameters in requestlocationupdates you should probably make huge so you don't get locationupdates since you don't really care about those, only provider enabled/disabled changes.
Please use this code to get the GPS status. use this code in the onResume of the activity
private LocationManager mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
boolean GPSprovider = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
and according to the GPSProvider you can enable and disable the button.
try this
final String GpsProvider = Settings.Secure.getString(
getContentResolver(),
Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
if (GpsProvider.equals(""){
//No GPS
}else{
//GPS available
}

Categories

Resources