2015-11-04 79 views
0

我想開發一些解決方案來使用Android的GPS獲取我當前的位置。但是,搜索僅限於超時,並且當搜索位置開始時,還有一個允許用戶取消搜索的對話框。如何通過Android GPS使用Runnables獲取當前位置並搜索TimeOut?

要做到這一點,我想使用可運行的。接下來是對定時器負責的代碼(我在具有開關情況的狀態機中使用它)。

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

刪除,

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;