我使用LocationManager
獲得一個位置修復:如何從LocationManager獲取單個位置修復程序,並且超時?
public class MyActivity extends Activity {
private LocationManager lm;
private ProgressDialog myDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 1, ll);
myDialog = ProgressDialog.show(this, null , "Determining Your Location", true);
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
if (location != null) {
...
lm.removeUpdates(this);
myDialog.dismiss();
}
}
}
}
由於這代表聆聽位置可能永遠持續下去,如果位置鎖定無法找到。我想通過在60秒之後停止監聽位置並向用戶顯示錯誤以表明他們的位置不能被確定來爲我的代碼添加一些健壯性。
我該怎麼做?