您可以通过三种不同的方式使用 ProgressDailog - 使用线程、处理程序和异步任务。这是使用进度对话框的异步任务示例
private class Operation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params)
{
// code to be executed in background thread
for(int i=0;i<5;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
// runs on UI thread and updated UI after executing doInBackground
progressDialog.dismiss();
}
@Override
protected void onPreExecute() {
ProgressDialog progressDialog = ProgressDialog.show(MainActivity.this, "Title ", "Loading...");
progressDialog.show();
}
@Override
protected void onProgressUpdate(Void... values) {
// runs on UI thread and starts first
}
}