【问题标题】:Remove Location Listener删除位置侦听器
【发布时间】:2014-08-27 09:21:06
【问题描述】:

我想在打开对话框并单击位置按钮后,检测用户的位置并显示用户的位置,然后用户关闭对话框,位置侦听器删除而不是从 onLocationChanged 获取位置,我尝试使用 locationManager.removeUpdate(locListener)on 覆盖 dismiss但再次解雇后没有任何机会和位置!为什么?如何在显示对话框后和检测位置后检测用户的位置,删除位置侦听器?谢谢

编辑:

public class DialogTest extends Dialog implements
android.view.View.OnClickListener {
Context context;
LocationManager locationManager;
LocationListener locationListener;

Location location;

public DialogTest(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.context = context;
    setContentView(R.layout.profile_dialog);

    getWindow().setBackgroundDrawableResource(R.drawable.background_dialog);

    firstAccessLocation=true;

    setCancelable(true);
    initialize();
}
@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub
    findAddress();
}

private void findAddress() {
    // if (gpsCheckBox.isChecked()) {
    locationManager = (LocationManager) context
            .getSystemService(Context.LOCATION_SERVICE);
    locationListener = new GPSLocationListener();
    locationManager.requestLocationUpdates(locationManager.GPS_PROVIDER, 0,
            0, locationListener);
    locationManager.requestLocationUpdates(
            locationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    // }
}

private void stopTracking(){
    if(locationManager!=null){
        locationManager.removeUpdates(locationListener);
    }
}

@Override
public void dismiss() {
    // TODO Auto-generated method stub
    stopTracking();
    gpsTurnOff();
    super.dismiss();
}

public class GPSLocationListener implements LocationListener {

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        if(location!=null)
            Log.e("location", location.getLatitude()+"");
        location = loc;
        if (location != null&& firstAccessLocation) {
            setAddress(location.getLatitude(), location.getLongitude());
            firstAccessLocation=false;
        }
        Toast.makeText(context, "changed", Toast.LENGTH_LONG).show();
        Log.e("location", "changed");
    }

    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

}

【问题讨论】:

  • 你能发布一些你当前的代码吗?
  • 谢谢。我在我的问题中添加了一些代码。

标签: android dialog location


【解决方案1】:

在新代码中,您应该使用LocationClient 而不是LocationManager。以最小的能量(电池消耗)更准确地获得位置更有效。看这里LocationClient vs LocationManager。建立LocationClient 后,您可以致电getLastLocation(),它会为您提供最后的位置,并且不会开始循环位置更新。 如果您想坚持使用LocationManager,请使用requestSingleUpdate (String provider, LocationListener listener, Looper looper) 方法从中获取一个最新位置。

【讨论】:

  • 我想得到确切的用户位置而不是最后一个位置。最后位置有时不是正确的位置。我的问题是,我想删除更新检测位置,但我不知道该怎么做。
  • 所以就像我在编辑中写的那样,使用requestSingleUpdate 方法,您将不必删除位置更新。
  • requestSingleUpdate 不可用!我应该在 GPSLocationListener 或 DialogTest 类中使用它吗?
  • public void requestSingleUpdate (String provider, LocationListener listener, Looper looper) 是 LocationManager 的一种方法,因此请在代码中使用它而不是 requestLocationUpdates
  • 谢谢。什么是活套?这个怎么填?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-02
  • 2018-09-04
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
相关资源
最近更新 更多