如何在我的LocationListener中停止onLocationChanged(),当我按下按钮时,onLocationChanged中的Toastbox不断出现,即使我离开应用程序也无法停止。
Button btn_location =(Button) findViewById(R.id.button4);
btn_location.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
}
});
public class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc)
{
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " + "Latitud = " + loc.getLatitude() + "Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
以下代码对我有用:
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
aLocationManager.removeUpdates(aLocationListener);
}
只要在想要停止更新的地方调用removeUpdates()方法即可!