How can I close a ProgressDialog after a set time?

谁说胖子不能爱 提交于 2019-12-03 14:41:56

AsyncTask is okay if you are processing a long running task and then want to cancel it after, but it's a bit overkill for a 3 second wait. Try a simple handler.

    final ProgressDialog progress = new ProgressDialog(this);
    progress.setTitle("Connecting");
    progress.setMessage("Please wait while we connect to devices...");
    progress.show();

    Runnable progressRunnable = new Runnable() {

        @Override
        public void run() {
            progress.cancel();
        }
    };

    Handler pdCanceller = new Handler();
    pdCanceller.postDelayed(progressRunnable, 3000);

Updated adding show/hide:

progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        theLayout.setVisibility(View.GONE);
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!