public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadData();
setContentView(R.layout.preload);
Thread thread = new Thread()
{
@Override
public void run() {
setUpDB();
setContentView(R.layout.main);
}
};
thread.start();
}
所以我希望它在数据库设置时显示预加载视图(setUpDB),然后在安装完成后切换到主视图切换到主视图。我几乎工作了;显示加载程序直到大约80%然后崩溃,但现在我甚至无法让它显示加载器...如果我在线程中有setUpDB()崩溃,如果我在线程外它显示一个空白屏幕直到满载。我记不起以前的代码......有什么想法吗?
答案 0 :(得分:4)
尝试使用assynctask ..
private class LoadAssync extends AsyncTask<String, Void, Void> { // Assync task
protected void onPreExecute() {
ProgressDialog dialog=ProgressDialog.show(this,"","Loading");
}
protected Void doInBackground(final String... args) {
setUpDB();
}
protected void onPostExecute(final Void unused) {
if (dialog.isShowing()) {
dialog.dismiss();
}
}
}
执行assynctask
LoadAssync mAsyync1;
mAsyync1 = new LoadAssync();
mAsyync1.execute(null);
答案 1 :(得分:2)
声明一个处理程序并调用它来更新你的UI,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ProgressDialog pd=ProgressDialog.show(this,"","Please Wait...");
Handler handler =new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0)
{
pd.dismiss();
setContentView(R.layout.main);
}
}
};
Thread thread = new Thread()
{
@Override
public void run() {
loadData();
setUpDB();
handler.sendEmptyMEssage(0);
}
};
thread.start();
}
答案 2 :(得分:0)
setUpDB();
splashHandler.sendEmptyMessageDelayed(STOP_SPLASH, STOP_SPLASH_DELAY);
private Handler splashHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case STOP_SPLASH:
progressbar.dismiss();
startActivity(new Intent(getApplicationContext(),
HomeScreenActivity.class));
finish();
break;
}
};
};