您的活動創建的AsyncTask對象,並調用execute()方法。 CustomAsyncTask是內部類。
ProgressDialog mProgressDialog; mProgressDialog = new ProgressDialog(PlayerView.this); CustomAsyncTask asyncTask = new CustomAsyncTask(); asyncTask.execute();
private class CustomAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.setMessage("Loading");
mProgressDialog.setTitle("please wait");
mProgressDialog.show();
}
@Override
protected ArrayList<PlayerViewModel> doInBackground(Void... params) {
return //do yout doInBackground work here
}
@Override
protected void onPostExecute(Void void) {
//when your process completed hide the ProgressDialog
mProgressDialog.hide();
}
}
你要做的是,開始在onPreExecute()方法的進度對話框並隱藏progressDialog在onPostExecute方法,因爲這兩種方法都在主Thred工作。
您需要在您的問題中包含代碼,以便我們能夠理解實際發生的情況 – Max
如果'OnPreExecute'中調用'ProgressDialog.show()',那麼您肯定會在'AsyncTask'上運行一些阻塞代碼。僅僅因爲你使用'AsyncTask'並不意味着所有的代碼都在後臺運行 – SoroushA