启动屏幕后,在主活动中加载oncreate内容大约需要6秒。所以我想在加载时显示一个进度对话框,下面是我所做的:
import ...
private ProgressDialog mainProgress;
public void onCreate(Bundle davedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgress = new ProgressDialog (Main.this);
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMessage("Loading... please wait");
mProgress.setIndeterminate(false);
mProgress.setMax(100);
mProgress.setProgress(0);
mProgress.show();
---some code---
mProgress.setProgress(50);
---some code---
mProgress.setProgress(100);
mProgress.dismiss();
}
它不起作用…屏幕保持黑色5-6秒,然后加载主布局。我不知道我做错了哪一部分:*(
最佳答案
您需要在另一个线程(而不是ui线程)中启动繁忙的代码。
在活动中创建progressdialog并在线程中关闭它
mProgress = new ProgressDialog (Main.this);
Runnable runnable = new Runnable() {
@Override
public void run() {
// code that needs 6 seconds for execution
// after finishing, close the progress bar
mProgress.dismiss();
}
};
new Thread(runnable).start();