我想在线程完成任务后使用 setContentView() 方法。但是我怎么能意识到这一点,因为它不可能在线程内使用这种方法?在线程运行时在 onCreate() 方法中使用此方法时,我也没有得到正确的结果,因为不显示第一个“setContentView(R.layout.load_screen)”的布局。
我的 onCreate() 方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Open loading screen
setContentView(R.layout.load_screen);
this.loadingScreen(); // In this method the new Thread will start.
}
我的 loadingScreen() 方法:(线程完成后我想再次使用 setContentView())
public void loadingScreen(){
// prepare for a progress bar dialog
progressBar = new ProgressBar(this);
progressBar = (ProgressBar)findViewById(R.id.progressBar1);
//reset progress bar status
progressBarStatus = 0;
new Thread(new Runnable() {
public void run() {
while (progressBarStatus < 100) {
try {
Thread.sleep(100);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// process some tasks
progressBarStatus = doSomeTasks();
// your computer is too fast, sleep 1 second
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressBarStatus);
}
});
}
// ok, file is downloaded,
if (progressBarStatus >= 100) {
// sleep 2 seconds, so that you can see the 100%
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}