您可以创建两个不同的LocationListener
:
private LocationListener gpsListener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
if(location != null){
// do your work for gps location
}
}
};
private LocationListener networkListener = .... // same init as gpsListener
并使用如下:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5, gpsListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 500, 5, networkListener);
然后您可以使用您正在执行的方法,而不会丢失 GPS 定位。代替:
locationManager.removeUpdates(this);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 5,this);
您将使用:
locationManager.removeUpdates(networkListener);