你好,有人可以帮我解决我做错了什么。
我希望这个应用程序在后台工作,我只想在一个按钮中停止位置管理器。当我使用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 :(得分: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()
答案 1 :(得分:0)
locationManager.removeUpdates(MyLocationListener);
试试这个。