【问题标题】:Progress Dialog location listener进度对话框位置监听器
【发布时间】:2013-03-26 12:44:44
【问题描述】:

在我的程序中,我有一个与 GPS 一起使用的位置侦听器来获取用户当前的纬度/经度点。

我想在 GPS 获得坐标的同时实现一个进度对话框。

目前我在 onCreate() 方法中调用 progressDialog,然后当我的位置对象不再为 null 时,我会取消 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);

            }
        }

【问题讨论】:

  • 你到底想做什么?关闭 ProgressDialog 后是否要显示另一个对话框?
  • @Grishu 我想在搜索 GPS 纬度/经度值的同时启动一个 progressDialog。一旦找到它们,即位置值不为空,我希望它关闭。

标签: java android progressdialog


【解决方案1】:

使用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;


                }
            }

【讨论】:

  • 这是使用 AsyncTask 的好方法
猜你喜欢
  • 2021-07-22
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多