2013-03-26 80 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