2013-03-26 114 views
0

在我的程序中,我有一個與GPS一起使用的位置列表器,用於獲取用戶當前的經緯度點。進度對話框位置監聽器

我想實現一個進度對話框,而GPS獲得座標。

目前,我調用onCreate()方法內的progressDialog,然後當我的位置對象不再空,然後我dismess progressdialog。

很遺憾,目前對話框並沒有顯示出來。

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 



    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  

     locationListener = new GPSLocationListener(); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

***** Call a new progress dialog object when the locationManager is gaining lat/long***** 
d = ProgressDialog.show(this, "GPS Posistion", "Gaining GPS posistion...", false, true); 


} 

    private class GPSLocationListener implements LocationListener 
    { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 

       ***** Once lat/long is found, dismiss the progress dialog***** 
       d.dismiss(); 

       Double latToPass = location.getLatitude(); 
       Double longToPass = location.getLongitude(); 

       locationManager.removeUpdates(locationListener); 
       locationManager = null; 

       Intent changesStart = new Intent("com.example.flybaseapp.PassLatLong"); 
       changesStart.putExtra("passedLat", latToPass); 
       changesStart.putExtra("passedLong", longToPass); 
       startActivity(changesStart); 

      } 
     } 
+0

你想要做什麼?解除ProgressDialog後,你想顯示另一個對話框嗎? – GrIsHu 2013-03-26 12:49:09

+0

@Grishu我simlpy想要啓動一個progressDialog,同時搜索GPS經緯度值。一旦發現它們的位置值不爲空,我希望它解僱。 – user1352057 2013-03-26 13:10:21

回答

0

使用AsyncTask

Double latToPass; 
    Double longToPass; 


protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

new AsyncAction().execute(null, null, null); 

} 





    private class AsyncAction extends AsyncTask<String, Void, String> { 
      public boolean status = false; 
      private ProgressDialog pd; 

      @Override 
      protected String doInBackground(String... arg0) { 
       // TODO Auto-generated method stub 
       try { 
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  

      locationListener = new GPSLocationListener(); 

      locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 


        status = true; 

       } catch (Exception e) { 
        // TODO: handle exception 
       } 

       return null; 
      } 

      @Override 
      protected void onPostExecute(String result) { 

       pd.dismiss(); 

      Intent changesStart = new Intent("com.example.flybaseapp.PassLatLong"); 
        changesStart.putExtra("passedLat", latToPass); 
        changesStart.putExtra("passedLong", longToPass); 
        startActivity(changesStart); 


       } 
      } 

      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
       super.onPreExecute(); 
       pd = new ProgressDialog(MainActivity.this); 
       pd.setMessage("loading..."); 
       pd.setIndeterminate(true); 
       pd.setCancelable(false); 
       pd.show(); 
      } 

     } 


     private class GPSLocationListener implements LocationListener 
     { 
      @Override 
      public void onLocationChanged(Location location) { 
       if (location != null) { 



        latToPass = location.getLatitude(); 
        longToPass = location.getLongitude(); 

        locationManager.removeUpdates(locationListener); 
        //locationManager = null; 


       } 
      } 
+1

這是使用AsyncTask的好方法 – user1352057 2013-03-26 13:13:00