2012-07-06 79 views
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(); 
    } 

} 

我真的需要這種嚴重

回答

0

的更改此代碼(*表示什麼改變*):

***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()

+0

locationListener無法解析爲一個變量..這就是它說當我測試你的代碼 – 2012-07-10 12:09:34

0
locationManager.removeUpdates(MyLocationListener); 

「與大寫字母開頭的類」試試這個「與小字母開頭的全局變量」。

+0

對不起,一切都不工作。 MyLocationListener無法解析爲變量... – 2012-07-11 06:17:24