在启动活动期间加载数据库缓慢

时间:2013-07-26 12:31:02

标签: android android-sqlite

我在我的启动活动中使用数据库。但是每次启动时,我的应用程序都比上一次启动时间更长。请帮我解决问题。

private CreateDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);
    Thread t = new Thread() {
        public void run() {
            try {
                db = new CreateDatabase(Splash.this);
                final SQLiteDatabase w = db.getWritableDatabase();
                w.execSQL("insert into Fin values( '" +"AAAAAAA"+ "','" +"BBBBBBBBBBBBBBBBB"+ "',  '" +0+ "')");
                w.execSQL("insert into Fin values( '" +"CCCCCCCCCCCC"+ "','" +"DDDDDDDDD"+ "',  '" +12+ "')");
                w.close();
                db.close();
                sleep(5000);
                Intent it = new Intent("pack1.exp.DASHBOARD");
                startActivity(it);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                finish();
            }
        }
    };
    t.start();
}

3 个答案:

答案 0 :(得分:1)

由于代码保持不变,因此启动加载的唯一区别是表 Fin 的填充。通过检查索引等来检查此表是否为optimized properly。可能是每次加载时查询变得越来越慢,特别是如果它有许多索引和大量数据。

答案 1 :(得分:1)

只有在查询时间少于5秒时才应该睡眠。除此之外,我不确定为什么查询每次启动都需要花费更多时间。

private CreateDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread t = new Thread() {
    public void run() {
        try {
            long timeStart = System.currentTimeMillis();
            db = new CreateDatabase(Splash.this);
            final SQLiteDatabase w = db.getWritableDatabase();
            w.execSQL("insert into Fin values( '" +"AAAAAAA"+ "','" +"BBBBBBBBBBBBBBBBB"+ "',  '" +0+ "')");
            w.execSQL("insert into Fin values( '" +"CCCCCCCCCCCC"+ "','" +"DDDDDDDDD"+ "',  '" +12+ "')");
            w.close();
            db.close();
            if((System.currentTimeMillis() - timeStart) < 5000){
            sleep(5000 - (System.currentTimeMillis() - timeStart)); //Only sleep if the database query takes less than 5 seconds
            }
            Intent it = new Intent("pack1.exp.DASHBOARD");
            startActivity(it);
            finish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
};
t.start();
}

答案 2 :(得分:0)

我认为这会很有用,显示你想要的文字和旋转轮的进度对话框。

    private void startProgressDialogTimer() {
final ProgressDialog progressDialog = ProgressDialog.show(
        context, "",
        context.getString(R.string.my_text_to_show));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        progressDialog.dismiss();
        handler.removeCallbacks(this);
    }
}, 5000);// show progressDialog for 5 secound
        }


       private CreateDatabase db;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.splash);
    try {
        db = new CreateDatabase(Splash.this);
        final SQLiteDatabase w = db.getWritableDatabase();
        w.execSQL("insert into Fin values( '" +"AAAAAAA"+ "','"   +"BBBBBBBBBBBBBBBBB"+ "',  '" +0+ "')");
        w.execSQL("insert into Fin values( '" +"CCCCCCCCCCCC"+ "','" +"DDDDDDDDD"+ "',  '" +12+ "')");
        w.close();
        db.close();
        //sleep(5000); -- remove this
        startProgressDialogTimer //-- add this
        Intent it = new Intent("pack1.exp.DASHBOARD");
        startActivity(it);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        finish();
    }

}