如何显示启动画面,同时初始化数据库? [英] How to show splash screen while initializing database?

查看:91
本文介绍了如何显示启动画面,同时初始化数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个闪屏,它完美的作品。现在我想同时显示开机画面加载数据库和数据库已加载后完全显示应用程序的UI。
我有以下的code要做到这一点这是正确的?

I have created an splash screen, which works perfectly. Now I want to load database while showing splash screen and after database has been loaded completely show the application UI.
I have the following code to do this is this right?

public class Splash extends Activity{

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    new LoadDatabase().execute();
}

@Override
protected void onPause() {
    super.onPause();
    finish();
}

// Load DB
protected class LoadDatabase extends AsyncTask<Context, Integer, String> 
{ 
     @Override 
     protected String doInBackground( Context... params ) { 
         try {
            new DatabaseHelper(getApplicationContext()).initializeDatabase();
        } catch (IOException e) {
            e.printStackTrace();
        }
         return "";
     } 

     @Override 
     protected void onPreExecute() { 
        super.onPreExecute(); 
        setContentView(R.layout.splash);
     } 

     @Override
    protected void onPostExecute(String result) {           
        super.onPostExecute(result);
         Intent openMain = new Intent("com.nepways.MAIN"); 
         startActivity(openMain); 
    }
}

}


有没有什么好的例子或建议?请帮我出这一点。


Is there any good example or suggestions?? please help me out with this.

推荐答案

您可以通过异步或线程和处理程序

You may perform this operation through async or thread and handler

protected class MyTask extends AsyncTask<Context, Integer, String> 
{ 
     @Override 
     protected String doInBackground( Context... params ) 
     { 
      // write ur database functionality 
     } 

     // -- gets called just before thread begins 
     @Override 
     protected void onPreExecute() 
     { 
        // u may show ur progressbar or spalsh screen
        super.onPreExecute(); 

     } 


     // -- called as soon as doInBackground method completes 
     @Override 
     protected void onPostExecute() 
     { 
       super.onPostExecute(result); 
      // Intent openMain = new Intent("com.nepways.MAIN"); 
            startActivity(openMain); 
     } 
} 

试试这个......可能是它会帮助U

Try this ... may be it will help u

这篇关于如何显示启动画面,同时初始化数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆