我试图通过获取所有侦听器来获取设备位置:
LocationManager locationManager = (LocationManager) myContext.getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
for (String s : locationManager.getAllProviders()) {
locationManager.requestLocationUpdates(s, checkInterval,
minDistance, new LocationListener() {
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
// if this is a gps location, we can use it
if (location.getProvider().equals(
LocationManager.GPS_PROVIDER)) {
doLocationUpdate(location, true);
stopGPS();
}
}
@Override
public void onStatusChanged(String provider,
int status, Bundle extras) {
// TODO Auto-generated method stub
}
});
gps_recorder_running = true;
}
// start the gps receiver thread
gpsTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Location location = getBestLocation();
doLocationUpdate(location, false);
if ((System.currentTimeMillis()-startMillis)>maxCheckTime){stopGPS();}
}
}, 0, checkInterval);
}
当我想停止侦听器时,问题就来了。我试图取消计时器:
gpsTimer.cancel();
但这并不能阻止听众。我想我必须使用 locationManager.removeUpdates,但是如何停止所有 Listeners?
谢谢