2014-09-11 28 views
2

我想要做的就是在移動到下一個活動之前使用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); 

但是該應用在這種情況下崩潰。需要快速修復。

以下是錯誤日誌::(在看看錯誤日誌之前,請看看在評論)

enter image description here

+0

「應用程序崩潰」 - > logcat的 – 2Dee 2014-09-11 08:46:15

回答

0

隨着代碼的東西基本上是錯誤的:的一行代碼在onLocationChanged

locationManagerAsync.requestLocationUpdates(providerAsync, 0, 0, this); 

需要在doInBackground結束(這並不需要在後臺完成的)被調用。這一行代碼請求來自提供者(例如GPS提供者)的位置更新。當新的位置可用時,您的位置監聽器的onLocationChanged被調用(在您的示例中爲MyLocationTracker,這是由於上述行中的this。在onLocationChanged中,您需要通過啓用或禁用功能來響應新位置, 。你的問題

getLastKnownLocation呼叫確實exectly什麼顧名思義:它返回最後已知的位置,這可能是很老

+0

我打過電話了onLocationChanged(位置。位置)只是doInBackground(), 的return語句但是同樣的崩潰問題仍然存在,之前的方法。讓我知道如果我做錯事.. – 2014-09-11 12:32:27

+1

你不需要調用onLocation。你需要調用requestLocationUpdates(但不是來自onLocation)。只要有新的位置可用,就會自動調用onLocation,並且需要插入代碼以對新位置作出反應。 – Stefan 2014-09-12 10:16:36

+0

我做了以下事情: 1.將以下內容放在onLocationCanged() - latAsync = location.getLatitude(); lonAsync = location.getLongitude(); 2. \t location = locationManagerAsync.getLastKnownLocation(providerAsync); \t \t //初始化位置字段 \t if(location!= null){locationManagerAsync.requestLocationUpdates(providerAsync,0,this,this); 但應用程序還是會被儘快活動啓動死機了,我叫的AsyncTask從onCreate()方法.. – 2014-09-12 12:18:45

相關問題