Android - 非UI线程无法正常运行 - 主ui线程中的异步线程饱和单例

时间:2016-02-22 16:41:28

标签: android multithreading android-sqlite

我有一个很大的sql数据库,所以我在非ui线程上做sql工作。但是,它似乎总是在ui线程上运行非ui线程。

/* MyDBHelper is singleton */
public class MyDBHelper extends SQLiteOpenHelper{

    /* Singleton code here*/

    @Override
    public void onCreate(SQLiteDatabase db) {
        buildDatabaseFromFile(db, "create.sql");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        buildDatabaseFromFile(db, "update.sql");
    }

    private void buildDatabaseFromFile(final SQLiteDatabase db, final String filePath) {
        onSqlExecution = true;
        new Thread(new Runnable() {
            /* sql code here + callback in main ui activity */
        }).start();
    }
}

public class MainActivity() extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        MyDBHelper.getInstance();
    }

    @Override
    protected void onResume() {
        if (onSqlExecution)
            /* new progress dialog here */
    }

    void callback() {
        /* run on ui thread : dismiss progress dialog here */
    }
}
你能帮帮我吗?感谢。

0 个答案:

没有答案