我正在尝试使用Android的GPS开发一些解决方案来获取当前位置。但是,搜索仅限于超时,并且当搜索位置开始时,还有一个允许用户取消搜索的Dialog。
要做到这一点,我正在考虑使用runnable。下面是负责定时器的代码(我在具有switch-case的状态机中使用它。)
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog = new ProgressDialog(AddPandlet.this);
progressDialog.setButton(DialogInterface.BUTTON_NEUTRAL, getString(R.string.addpandlet_cancelsearch),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//CANCEL OBTAINING GPS
return;
}
});
progressDialog.show();
scanLocation();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.hide();
}
});
status = Status.FINISH;
updateStateMachine();
}
}, SCAN_PERIOD_GPS);
在LocationListener上我这样做:
LocationListener locationListenerNetwork = new LocationListener() {
public void onLocationChanged(Location location) {
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerGps);
longitude = "Longitude: " + location.getLongitude();
latitude = "Latitude: " + location.getLatitude();
}
public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
};
此外,我无法获得GPS坐标,因为GPS始终在侦听,并且在计时器结束时不会停止。
有人能给我一些帮助吗?感谢。
答案 0 :(得分:0)
删除,
locationManager.removeUpdates(this);
locationManager.removeUpdates(locationListenerGps);
longitude = "Longitude: " + location.getLongitude();
latitude = "Latitude: " + location.getLatitude();
试试这个
longitude = "Longitude: " + location.getLongitude();
latitude = "Latitude: " + location.getLatitude();
locationManager.removeUpdates(locationListenerNetwork);
locationManager = null;