首先,您可以使用AsyncTask来执行需要很长时间的进程。如果你不了解它, it allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
但是如果你坚持不使用它,那么由于你阻塞了 UI 线程,你就不能在显示对话框的同时做你的事情。您需要有一个后台线程来处理冗长的进程,并在 UI 线程上显示进度对话框。
AsyncTaks
网上的例子很多。仅用于示例:
private class OuterClass extend Activity{
//....
@Override
public void onCreate(Bundle savedInstanceState) {
new performBackgroundTask ().execute();
}
//....
private class performBackgroundTask extends AsyncTask < Void, Void, Void >
{
private ProgressDialog dia;
// This method runs in UI thread before the background process starts.
@Override
protected void onPreExecute(){
// Show dialog
dia = new ProgressDialog(OuterClass.this);
dia.setMessage("Installing...");
dia.show();
}
@Override
protected Void doInBackground(Void... params) {
// Do all the stuff here ...
addQuickActions();
}
// Ececutes in UI thread after the long background process has finished
@Override
protected void onPostExecute(Void result){
// Dismiss dialog
dia.dismiss();
}
}
}
您可能会看到如何在 Android 中启动活动之前显示进度对话框?
希望这可以帮助。