Android : Android LocationManager.removeUpdates(LocationListener)

on Sunday, July 6, 2014


hi guys I have created a background service which gets the person location and if the distance between the device to the destination is smaller i request location updates more often and I am using the removeUpdates(LocationListener) method... the problem is that i noticed that the listener keeps getting updates and the more the loop is going it keeps get more and more updates. does anyone have any idea why this method doesn't work?


Here is my method for using the new location.



void makeUseOfNewLocation(Location location){
manager.removeUpdates(listener);
mLocation = location;
currentLat = location.getLatitude();
currentLong = location.getLongitude();
Location.distanceBetween(currentLat, currentLong, gateLat, gateLong, results);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
currentDistanceFromDestination = results[0];
Log.d("Current Location", "Lat:"+currentLat+" Long:"+currentLong);
Log.d("Destination Distance", currentDistanceFromDestination+"");
if(currentDistanceFromDestination<3000){
if(currentDistanceFromDestination<50){
Log.d("50m closer", "Calling");

startActivity(callIntent);
stopSelf();
}
if(currentDistanceFromDestination<800){
Log.d("800m closer", "Started listening every 10 seconds.");
Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10 ,90 , listener);
}else{
Log.d("3000m closer", "Started listening every 25 seconds");
Toast.makeText(getApplicationContext(),currentDistanceFromDestination+"", Toast.LENGTH_LONG).show();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25 * 1000, 300, listener);
}
}

}

0 comments:

Post a Comment