且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

进度对话框开放活动

更新时间:2023-01-25 18:08:41

我有同样的问题,使用的 AsyncTask的是为我工作。

I had the same issue and using an AsyncTask is working for me.

有在AsyncTask的覆盖三个重要的方法。

There are 3 important methods to override in AsyncTask.

  1. doInBackground :这就是你的背景肉 会发生处理。
  2. 在preExecute :在这里展示您的ProgressDialog(的ShowDialog)
  3. onPostExecute :这里隐藏ProgressDialog(removeDialog或dismissDialog )
  1. doInBackground : this is where the meat of your background processing will occur.
  2. onPreExecute : show your ProgressDialog here ( showDialog )
  3. onPostExecute : hide your ProgressDialog here ( removeDialog or dismissDialog )

如果你让你的AsyncTask子类作为一个内部类的活动,那么你可以从你的AsyncActivity中调用框架方法的ShowDialog,dismissDialog和removeDialog。

If you make your AsyncTask subclass as an inner class of your activity, then you can call the framework methods showDialog, dismissDialog, and removeDialog from within your AsyncActivity.

下面是AsyncTask的样本实现:

Here's a sample implementation of AsyncTask:

class LoginProgressTask 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(HelloAndroid.this, LandingActivity.class);
    startActivity(i);
  }
}