如何通过Android GPS使用Runnables获取当前位置并搜索TimeOut?

如何通过Android GPS使用Runnables获取当前位置并搜索TimeOut?

问题描述:

我想开发一些解决方案来使用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总是听,也不要当计时器结束停止。

有人可以给我一些帮助吗?谢谢。

删除,

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;