Progress dialog in Android

2019-06-10 08:14发布

问题:

I am fully aware on how to display progress dialogues in my application. I just want to know if there is anyway that we can set a "timeout" interval for this progress dialog. [I mean is there any API for this]

I can always run a thread for this, but thought it would be better if there was an inbuilt API already..

回答1:

There is no built-in api. Just use AsyncTask or thread, how you have already mentioned.



回答2:

You can use a Handler as well (post a delayed message to the handler and the handler will close the dialog). I don't know any way to tell the dialog to close itself after a given time.

private ProgressDialog dialog;
private Handler closeHandler = new Handler() {
  public void handleMessage(Message msg) {
    if (dialog!=null) dialog.dismiss();
  }
};


public void openDialog() {
  // Open the dialog

  // Close it after 2 seconds
  closeHandler.sendEmptyMessageDelayed(0, 2000);
}