web-dev-qa-db-ja.com

設定した時間後にProgressDialogを閉じるにはどうすればよいですか?

3秒後にProgressDialogボックスを自動的に閉じようとしています。ダイアログは次のとおりです。

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

いくつかの方法を試しましたが、どれも機能させることができません。簡単な時間か何かで十分だといいのですが。ありがとう。

7
Holdfast33

AsyncTaskは、長時間実行されているタスクを処理していて、後でキャンセルしたい場合は問題ありませんが、3秒間待つのは少しやり過ぎです。単純なハンドラーを試してください。

    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);

表示/非表示の追加を更新:

progress.setOnCancelListener(new DialogInterface.OnCancelListener() {
    @Override
    public void onCancel(DialogInterface dialog) {
        theLayout.setVisibility(View.GONE);
    }
});
19
Gary Bak