如何在android中加载进度对话框?

时间:2011-06-07 17:19:49

标签: android progressdialog

我已经开发了一个Android应用程序。在该应用程序从Web获取信息并显示在屏幕上。在获取信息的时候,我想在获取我希望关闭对话框的信息后将进度对话框加载到屏幕

请任何人通过一些示例代码帮助我如何做到这一点

提前致谢

2 个答案:

答案 0 :(得分:2)

您需要实施AsyncTask。 示例:

class YourAsyncTask extends AsyncTask<Void, Void, Void> {

    private ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        //show your dialog here
        progressDialog = ProgressDialog.show(this, "title", "message", true, false)
    }

    @Override
    protected Void doInBackground(Void... params) {        
        //make your request here - it will run in a different thread
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        //hide your dialog here
        progressDialog.dismiss();
    }
}

然后你只需要打电话

new YourAsyncTask().execute();

您可以在此处详细了解AsyncTaskhttp://developer.android.com/reference/android/os/AsyncTask.html

答案 1 :(得分:0)

关键是,你应该使用两个不同的线程,第一个是UI线程,第二个是“加载数据线程” 从第二个线程,您将过程状态发布到第一个线程,例如:仍在工作或50%完成 use this to post data