我想要做的就是在移動到下一個活動之前使用GPS獲取當前位置。所以我禁用了將我帶到下一個活動的按鈕。只有在使用GPS獲取位置後才能啓用按鈕。以下代碼完美運行。但我發現了一個bug ..返回最後一個位置的GPS代碼
上次我使用該應用程序,它是非常正確的。然後我搬到了我的大學。在那裏,我啓動了應用程序,然後移動到下一個活動,因爲當時位置已恢復。我得到了我家10公里外的位置。然後關閉應用程序後,我再次啓動它,並且這次它檢索到正確的位置。
很好,這是因爲線下的發生:
location = locationManagerAsync.getLastKnownLocation(providerAsync);
我的應用程序被檢索的最後位置和啓用按鈕。但它能夠保留當前位置以供下次啓動應用程序時使用。結果,在第二次嘗試中,位置被完美地取回。
我使用的代碼來從下面給出上stackoverflow.com的問題。
private class MyLocationTracker extends AsyncTask<Void, Void, Void> implements LocationListener {
private Context ContextAsync;
public MyLocationTracker (Context context){
this.ContextAsync = context;
}
Dialog progress;
private String providerAsync;
private LocationManager locationManagerAsync;
double latAsync=0.0;
double lonAsync=0.0;
Location location;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(ContextAsync, "Loading location", "Please wait...");
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
locationManagerAsync = (LocationManager) ContextAsync.getSystemService(ContextAsync.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
providerAsync = locationManagerAsync.getBestProvider(criteria, false);
if (locationManagerAsync.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
providerAsync = LocationManager.GPS_PROVIDER;
} else if (locationManagerAsync.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
providerAsync = LocationManager.NETWORK_PROVIDER;
} else if (locationManagerAsync.isProviderEnabled(LocationManager.PASSIVE_PROVIDER)) {
providerAsync = LocationManager.PASSIVE_PROVIDER;
//Toast.makeText(ContextAsync, "Switch On Data Connection!!!!", Toast.LENGTH_LONG).show();
}
location = locationManagerAsync.getLastKnownLocation(providerAsync);
// Initialize the location fields
if (location != null) {
// System.out.println("Provider " + provider + " has been selected.");
latAsync = location.getLatitude();
lonAsync = location.getLongitude();
} else {
//Toast.makeText(ContextAsync, " Location not available", Toast.LENGTH_SHORT).show();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
onLocationChanged(location);
Log.v("latAsync_lonAsync",latAsync+"_"+lonAsync);
myCurrentLatitude = latAsync;
myCurrentLongitude = lonAsync;
// the buttons that were disabled in the onCreate() method
police.setEnabled(true);
medical.setEnabled(true);
fireService.setEnabled(true);
cityCorporation.setEnabled(true);
telePhnCall.setEnabled(true);
getRoute.setEnabled(true);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
這是我使用調用從OnCreate中的的AsyncTask()方法,行..
new MyLocationTracker(TypeOfEmergency.this).execute();
現在的問題是,我不希望最後的位置進行檢索並存儲在變量中。我在刪除該行後嘗試了代碼
location = locationManagerAsync.getLastKnownLocation(providerAsync);
但是該應用在這種情況下崩潰。需要快速修復。
以下是錯誤日誌::(在看看錯誤日誌之前,請看看在評論)
「應用程序崩潰」 - > logcat的 – 2Dee 2014-09-11 08:46:15