如何显示加载对话框

时间:2012-01-31 03:52:16

标签: android

我尝试使用以下代码加载网址。

URL url = new URL(urlstr);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setConnectTimeout(10000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
InputStream is = connection.getInputStream();  //spend lots of time

因为行InputStream is = connection.getInputStream();会花一些时间。 所以我想在加载时显示加载对话框。 我能做到吗?

在AActivity中,在代码下面调用BActivity。

Intent intent = new Intent(AActivity.this, BActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
Window w = MyGroup.group.getLocalActivityManager().startActivity("BActivity", intent);  
View view = w.getDecorView();  
MyGroup.group.setContentView(view);

BActivity是加载URL和提取信息。 加载代码位于onCreate()。

我尝试了答案代码,错误Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@2afe9488 is not valid; is your activity running?显示。

4 个答案:

答案 0 :(得分:3)

您可以使用asynctask show progress对话框实现如下

在Oncreate中:

         new GetTask(this).execute();//taken object for asyntask class.



    class GetTask extends AsyncTask<Object, Void, String> {
     {
        ProgressDialog progressDialog;
       void GetTask(Context cntxt)
      {

      }
        @Override
    protected void onPreExecute() {
        super.onPreExecute();

        mDialog = new ProgressDialog(cntxt); //taking object for progress dialog
        mDialog.setMessage("Please wait...");
        mDialog.show(); //Displaying progressDialog
    }

    @Override
    protected String doInBackground(Object... params) {

         //do the background process

        return ""; you can return string value

    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (mDialog != null) {
            mDialog.dismiss();//close the progress Dialog
        }

     }
      }

答案 1 :(得分:1)

你想要一个ProgressDialog。请参阅此link

答案 2 :(得分:1)

进度对话框

private ProgressDialog dialog;
    public  void showProgress () {
        dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setMessage("Please wait");
        dialog.show();

    }

使用异步任务进行下载...

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                //Do your downloading task

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
                 dialog.dismiss(); 
            }
        }

执行下载任务前调用进度对话框

showProgress();
        DownloadWebPageTask task = new DownloadWebPageTask();
        task.execute(new String[] { "http://www.url.com" });

答案 3 :(得分:1)

使用DownloadWebPageTask中的构造函数初始化上下文并在对话框中使用该上下文。

或使用yourclass.this in     dialog = new ProgressDialog(yourclass.this);