1

我在启动活动中使用数据库。但是每次启动我的应用程序都比之前的启动时间花费更多的时间。请帮我查询。

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();
}
4

3 回答 3

1

Since the code remains the same, the only difference in the loading of the splash is the population of the table Fin. Check that this table is optimized properly, by checking indexes etc. It might be that the query is getting slower to process with each load, especially if it has many indexes and a lot of data.

于 2013-07-26T13:17:33.170 回答
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();
}
于 2013-07-26T13:19:20.910 回答
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();
    }

}
于 2013-07-26T12:51:41.427 回答