2014-08-27 131 views
0

我想和点击位置按钮,检测用户的位置,并显示用户的位置,然后用户辞退对话框,位置监听器删除,并没有得到位置,我尝试覆盖dismiss但没有任何使用locationManager.removeUpdate(locListener)再次解散后的机会和位置显示!为什么?如何在显示对话框后检测用户的位置,并在检测到位置,位置侦听器删除后?由于删除位置监听器

编辑:

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 

    } 

} 

}

+0

你可以发布一些你当前的代码吗? – Niko 2014-08-27 09:23:15

+0

谢谢。我在我的问题中添加了一些代码。 – user3209380 2014-08-27 09:37:00

回答

0

在新的代码,你应该使用LocationClient而不是LocationManager。以更小的能量(电池消耗)以更高的精度获得位置效率更高。看这里LocationClient vs LocationManager。随着LocationClient建立,您可以拨打电话getLastLocation(),它为您提供最后一个位置,并且不启动周期性位置更新。 如果你想坚持使用LocationManager使用requestSingleUpdate (String provider, LocationListener listener, Looper looper)方法从它只获得一个最新的位置。

+0

我想得到确切的用户位置不是最后的位置。有时候最后的位置是不正确的位置。我的问题是,我想删除更新检测位置,但我不知道我该怎么做。 – user3209380 2014-08-27 09:50:32

+0

就像我在编辑中写的一样,使用'requestSingleUpdate'方法,你不必删除位置更新。 – 2014-08-27 09:51:41

+0

requestSingleUpdate不可用!我应该在GPSLocationListener或DialogTest类中使用它? – user3209380 2014-08-27 09:55:51