简体   繁体   English

在使用AsyncTask搜索Google Map的过程中显示进度对话框

[英]Showing the progress dialog during searching google map with AsyncTask

I would like to display a progress dialog when server is looking for the address on the google map, when the progress is finished, the dialog disappears. 当服务器在google map上查找地址时,我想显示一个进度对话框,进度完成后,该对话框消失。 I googled it and the most of result is talking about AsyncTask, however I am still confused about the parameter of the function doInBackground() and the usage of onPreExecute() and onPostExecute(). 我用谷歌搜索,结果最多的是谈论AsyncTask,但是我仍然对函数doInBackground()的参数以及onPreExecute()和onPostExecute()的用法感到困惑。 Could someone give me some solutions about that. 有人可以给我一些解决方案。 I really appreciate with any help, thanks. 非常感谢您的帮助。

protected void mapCurrentAddress() {
    String addressString = addressText.getText().toString();
    Geocoder g = new Geocoder(this);
    List<Address> addresses;
    try {
        addresses = g.getFromLocationName(addressString, 1);
        if (addresses.size() > 0) {
            address = addresses.get(0);
            List<Overlay> mapOverlays = mapView.getOverlays();
            AddressOverlay addressOverlay = new AddressOverlay(address);
            mapOverlays.add(addressOverlay);
            mapView.invalidate();
            final MapController mapController = mapView.getController();
            mapController.animateTo(addressOverlay.getGeopoint(), new Runnable() {
                public void run() {
                    mapController.setZoom(12);
                }
            });
            useLocationButton.setEnabled(true);
        } else {
            // show the user a note that we failed to get an address
            alert(this, addressString);
        }
    } catch (IOException e) {
        // show the user a note that we failed to get an address
        e.printStackTrace();
    }
}

private class SearchAddress extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(AddLocationMapActivity.this);

      // can use UI thread here
      @Override
      protected void onPreExecute() {
          this.dialog.setTitle("Checking");
          this.dialog.setMessage("Contacting Map Server...");
          this.dialog.show();
      }

      // automatically done on worker thread (separate from UI thread)
      @Override
      protected Void doInBackground(Void... params) {
          try {

              mapCurrentAddress(); 
          }
          catch(Exception e) {
              Log.e("DEBUGTAG", "Remote Image Exception", e);
          }
          return null;
      }

      // can use UI thread here
      @Override
      protected void onPostExecute(Void res) {
          if (this.dialog.isShowing()) {
              this.dialog.dismiss();
          }
      }

      @Override
      protected void onProgressUpdate(Void... values) {
          // TODO Auto-generated method stub
          super.onProgressUpdate(values);
      }

}
// this is the click event
mapLocationButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            new SearchAddress().execute(); // crash during doing the doInBackground
            //mapCurrentAddress(); // work perfectly
        }
    });

The application crashes after popup the progress dialog ? 弹出进度对话框后,应用程序崩溃了吗?

There are plenty of articles on asynctasks. 关于异步任务有很多文章。 Some of them I am mentioning here: http://developer.android.com/reference/android/os/AsyncTask.html 我在这里提到其中一些: http : //developer.android.com/reference/android/os/AsyncTask.html

http://www.vogella.de/articles/AndroidPerformance/article.html http://www.vogella.de/articles/AndroidPerformance/article.html

http://androidpartaker.wordpress.com/2010/08/01/android-async-task/ http://androidpartaker.wordpress.com/2010/08/01/android-async-task/

http://android10.org/index.php/articlesother/239-android-application-and-asynctask-basics http://android10.org/index.php/articlesother/239-android-application-and-asynctask-basics

and so on. 等等。 First brush up your async task concepts and start implementing them. 首先,整理一下异步任务概念并开始实施。 This will surely resolve your query. 这肯定会解决您的查询。

In Async we also have below method 在异步中,我们还有以下方法

@Override
protected void onProgressUpdate(Void... values) {
    // TODO Auto-generated method stub
    super.onProgressUpdate(values);
}

And from doInBackground() of Aysnc task you can update progress using below line: 从Aysnc任务的doInBackground()中,您可以使用下面的行更新进度:

publishProgress(10);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM