Android Gps禁用更新

时间:2014-05-27 09:46:13

标签: android gps

如何在点击停止按钮后禁用onLocationChanged()中的位置更新,同时还会不断更新位置。 我使用过这里提到的代码。 请帮我解决这个问题。

public class AndroidGPSSampleActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000; // in Milliseconds
protected LocationManager locationManager;
protected Button retrieveLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER, 
        MINIMUM_TIME_BETWEEN_UPDATES, 
        MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
        new MyLocationListener()
    );
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new MyLocationListener());

    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            showCurrentLocation();
        }
    });
}

protected void showCurrentLocation() {

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    if (location != null) {
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );

        Toast.makeText(AndroidGPSSampleActivity.this, "Sample test",
            Toast.LENGTH_LONG).show();
        Toast.makeText(AndroidGPSSampleActivity.this, message,
            Toast.LENGTH_LONG).show();
    }
}

private class MyLocationListener implements LocationListener {
    public void onLocationChanged(Location location) {
        String message = String.format(
            "New Location \n Longitude: %1$s \n Latitude: %2$s",
            location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(AndroidGPSSampleActivity.this, message, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String s, int i, Bundle b) {
        Toast.makeText(AndroidGPSSampleActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
    }

    public void onProviderDisabled(String s) {
        Toast.makeText(AndroidGPSSampleActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
    }

    public void onProviderEnabled(String s) {
        System.out.println("==onProviderEnabled=" + s);
        Toast.makeText(AndroidGPSSampleActivity.this, "Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
    }
}

1 个答案:

答案 0 :(得分:0)

我建议您将MyLocationListener作为类字段,稍后您可以从任何内部类访问它。您可以通过调用LocationManager类的removeUpdates方法来禁用特定侦听器的更新。

public class AndroidGPSSampleActivity extends Activity {

    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 30000;
    private MyLocationListener listener = new MyLocationListener();
    ...
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, listener);

可以使用以下代码在任何代码处停止更新:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.removeUpdates(listener);
    }
});