2014-08-27 134 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