与AsyncTask的搜索谷歌地图中显示的进度对话框 [英] Showing the progress dialog during searching google map with AsyncTask

查看:107
本文介绍了与AsyncTask的搜索谷歌地图中显示的进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示在服务器正在寻找谷歌地图,当进度完成的地址进度对话框,对话框消失。我GOOGLE了它和大多数结果都在谈论的AsyncTask,但我仍然困惑的功能doInBackground的参数()和preExecute()和onPostExecute()的用法。可能有人给我关于一些解决方案。我真的AP preciate任何帮助,谢谢。

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. 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(). 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 ?

推荐答案

有很多关于asynctasks文章。他们中的一些,我在此提出:
http://developer.android.com/reference/android/os/AsyncTask.html

There are plenty of articles on asynctasks. Some of them I am mentioning here: http://developer.android.com/reference/android/os/AsyncTask.html

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

HTTP://androidpartaker.word$p $ pss.com / 2010/08/01 / Android的异步任务/

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.

这篇关于与AsyncTask的搜索谷歌地图中显示的进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆