如何在Android中显示进度对话框?

如何在Android中显示进度对话框?,android,Android,我需要向用户显示进度消息。但我无法显示。这是我的代码。我的代码中有什么错误。请指导我如何操作 public class MyProgressDemo extends Activity { /** Called when the activity is first created. */ private Button clickBtn; public ProgressDialog progressDialog; Handler handler = new Handler(); @Overrid

我需要向用户显示进度消息。但我无法显示。这是我的代码。我的代码中有什么错误。请指导我如何操作

public class MyProgressDemo extends Activity {
/** Called when the activity is first created. */

private Button clickBtn;
public ProgressDialog progressDialog;
Handler handler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    clickBtn = (Button) findViewById(R.id.Button01);
    clickBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            progressDialog = ProgressDialog.show(MyProgressDemo.this, "",
                    "Please Wait");
            processThread();

        }

    });

}

protected void processThread() {

    handler.post(new Runnable() {

        @Override
        public void run() {
            longTimeMethod();
            UI();
            progressDialog.dismiss();
        }
    });

}

private void longTimeMethod() {
    try {
        String strMethod = "MethodName";
        String strUrl = "url";
        String strResponse = WebserviceCall.Mobileaappstore(strUrl,
                strMethod);
        Log.d("RES", strResponse);
    } catch (Exception e) {
        Log.e("Exc", e.getMessage());
    }

}

private void UI() {
    TextView tv = new TextView(this);
    tv.setText("This is new UI");
    setContentView(tv);
}

}

你想做的完全符合安卓系统的要求。尝试通过对AsyncTask类进行子类化来实现它,注意将所有与UI相关的内容放在onPreExecute/onPostExecute方法中,将long-time方法放在doInBackground方法中


如果您需要活动中的内容,可以将其作为参数传递到AsyncTask的构造函数中,或者将AsyncTask作为活动的内部类。

要添加到MarvinLabs的帖子中,您可以像这样显示并关闭ProgressDialog

private class SubmitCommentTask extends AsyncTask<String, Void, Void> {
    ProgressDialog dialog;
    protected Void doInBackground(String... params) {
        // Your long running code here
        return null;
    }

    protected void onPreExecute() {
        dialog = ProgressDialog.show(DetailsInfo.this, "Submitting Comment", "Please wait for the comment to be submitted.", true);
    }

    protected void onPostExecute(Void Result)
    {
        dialog.dismiss();
    }
}
私有类SubmitCommentTask扩展了AsyncTask{
进程对话;
受保护的Void doInBackground(字符串…参数){
//你的长时间运行的代码在这里
返回null;
}
受保护的void onPreExecute(){
dialog=ProgressDialog.show(DetailsInfo.this,“提交评论”,“请等待评论提交”,true);
}
受保护的void onPostExecute(void结果)
{
dialog.dismise();
}
}

方法Handler.post,在UI线程中生成一个线程,因此您的longTimeMethod();将在UI线程中运行,从而阻止它。你应该这样做:

protected void processThread() {
    Thread t = new Thread(){
       longTimeMethod();
       // Sends message to the handler so it updates the UI
       handler.sendMessage(Message.obtain(mHandler, THREAD_FINISHED));
    }
    // Spawn the new thread as a background thread
    t.start
}
为了管理消息,处理程序应该如下所示

private Handler mHandler = new Handler() {
    @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {
            case THREAD_FINISHED:
                       UI();
                       progressDialog.dismiss();
                       break    
                }   
        }
};
您可以使用此解决方案或AsynTask,这取决于您,两者都适用。选择最适合你的