我的progressDialog不会滚动?

时间:2012-09-04 08:43:52

标签: java android multithreading

当某些功能在后台运行时,我想显示一个循环progressdialog。所以我写了一系列代码:

    pd = ProgressDialog.show(GridLayoutActivity.this, "","...");
    new Thread(new Runnable() {
    public void run() {
        someFunc();
        handler.sendEmptyMessage(0);// 执行耗时的方法之后发送消给handler
    }
    }).start();

但progressDialog不会滚动?为什么呢?

3 个答案:

答案 0 :(得分:2)

尝试使用异步任务,在这里我发布示例代码,您可以按照自己的方式进行更改

public class BackGround_Task extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(
                YourClassName.this);

        // can use UI thread here
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }

        @Override
        protected Void doInBackground(Void... params) {
            //Your Background Task

            return null;
        }

        protected void onPostExecute(Void result) {

            //What will you do after the completion of Background process

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
        }
    }

答案 1 :(得分:0)

只需使用以下内容:

ProgressDialog progressDialog = new ProgressDialog(GridLayoutActivity.this);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.setMessage("...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();

new Thread(new Runnable() {
public void run() {
    someFunc();
    handler.sendEmptyMessage(0);// 执行耗时的方法之后发送消给handler
}
}).start();

答案 2 :(得分:0)

尝试阻止后发送处理程序消息。使用方式如下:

pd = ProgressDialog.show(SetFrames.this,"Loading", "Please Wait...",true);
           new Thread() {
               public void run() {
               try {
                 //your function
               }catch(Exception e)
               {
               }
               handler.sendEmptyMessage(0);
               pd.dismiss();
               }

               }.start();

private Handler handler = new Handler() {
        public void handleMessage(Message msg) {

        }
        };