1

您好,有人可以帮我解决我做错的事情。

我希望这个应用程序在后台工作,我只想在一个按钮中停止位置管理器。当我使用 removeUpdates 时,它不起作用。我不能在那里调用那个函数。

public class LbsGeocodingActivity extends Activity {

private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 60000; // Minuten(van milliseconden) * aantal

protected LocationManager locationManager;
protected LocationListener locationListener;

protected Button retrieveLocationButton;
protected Button stopLocationButton;

@Override
public void onCreate(Bundle savedInstanceState) {

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

    retrieveLocationButton = (Button) findViewById(R.id.retrieve_location_button);
    stopLocationButton = (Button) findViewById(R.id.stop_location_button);


    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);


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

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

    retrieveLocationButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
   LocationManager.removeUpdates(locationListener)  ;       }
    });      
} 


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(LbsGeocodingActivity.this, message,
                Toast.LENGTH_LONG).show();
    }

}   

public 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(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
    }

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

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

    public void onProviderEnabled(String s) {
        Toast.makeText(LbsGeocodingActivity.this,
                "Provider enabled by the user. GPS turned on",
                Toast.LENGTH_LONG).show();
    }

}

我真的很需要这个

4

2 回答 2

1

从此更改代码(* 表示更改的内容 *):

***retrieveLocationButton***.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    locationManager.removeUpdates(locationListener)  ;       
    }
}); 

对此:

stopLocationButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        locationManager.removeUpdates(locationListener)  ;       
    }
});   

编辑:

对函数使用locationManager“以小写字母开头的全局变量”而不是LocationManager“以大写字母开头的类”removeUpdates()

于 2012-07-08T10:44:56.150 回答
0
locationManager.removeUpdates(MyLocationListener);

尝试这个。

于 2012-07-10T13:43:07.997 回答