ProgressDialog解除开放的新活动

时间:2017-05-29 09:44:24

标签: java android progressdialog

如何在progressdialog dimiss上打开新活动?

以下是我的代码部分:

            @Override
        public void onClick(View v) {
            progressDoalog = new ProgressDialog(Hack.this);
            progressDoalog.setMax(100);
            progressDoalog.setMessage("Its loading....");
            progressDoalog.setTitle("ProgressDialog bar example");
            progressDoalog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDoalog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (progressDoalog.getProgress() <= progressDoalog
                                .getMax()) {
                            Thread.sleep(200);
                            handle.sendMessage(handle.obtainMessage());
                            if (progressDoalog.getProgress() == progressDoalog
                                    .getMax()) {
                                progressDoalog.dismiss();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

        Handler handle = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                progressDoalog.incrementProgressBy(1);
            }
        };
    });
}

请帮帮我们。我认为这是在progressdialog完成后开放活动的一种解决方案。

3 个答案:

答案 0 :(得分:1)

只需添加:

progressDoalog.setOnDismissListener(new DialogInterface.OnDismissListener(){
            @Override
            public void onDismiss(DialogInterface dialog) {
                //Launch your activity
            }
        });

这是一个简单的监听器,在你的对话解雇时调用

答案 1 :(得分:0)

您可以使用AsyncTask

在AsyncTask中有三种重要的方法可以覆盖。

  1. doInBackground :这是您进行背景处理的主要原因。
  2. onPreExecute :在此处显示您的ProgressDialog(showDialog)
  3. onPostExecute :在此处隐藏您的ProgressDialog(removeDialog或dismissDialog)
  4. 如果将AsyncTask子类作为活动的内部类,则可以在AsyncActivity中调用框架方法showDialog,dismissDialog和removeDialog。

    以下是AsyncTask的示例实现:

    class ProgressTask extends AsyncTask<String, Integer, Boolean> {
      @Override
      protected Boolean doInBackground(String... params) {
        try {
          Thread.sleep(4000);  // Do your real work here
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        return Boolean.TRUE;   // Return your real result here
      }
      @Override
      protected void onPreExecute() {
        showDialog(AUTHORIZING_DIALOG);
      }
      @Override
      protected void onPostExecute(Boolean result) {
        // result is the value returned from doInBackground
        removeDialog(AUTHORIZING_DIALOG);
        Intent i = new Intent(this, LandingActivity.class);
        startActivity(i);
      }
    }
    

答案 2 :(得分:0)

@MarkusMaleńki请参考下面的代码

if(progressDoalog!=null && progressDoalog.isShowing()){
  progressDoalog.setOnDismissListener(new 
  DialogInterface.OnDismissListener(){
  @Override
    public void onDismiss(DialogInterface dialog) {
    //Launch your activity
   }
});

}