如何在Android应用程序中使用SqlCipher lib

时间:2014-08-04 13:28:49

标签: android sql

我已经下载了Sqlcipher lib,它在应用程序中运行。

但我已经意识到使用sqlite open helper类的活动现在需要几秒钟才能加载,因为我在调用open helper类时加载了sqlcipher lib。

以下是代码的一部分......

private Context cnxt;
    public DbHelperClass(Context context) {
        super(context,DB_NAME,null, DB_VERSION);
        // TODO Auto-generated constructor stub
        cnxt= context.getApplicationContext();
        // load sqlcipher libs
        SQLiteDatabase.loadLibs(cnxt);
        this.getWritableDatabase(PWD);
    }

这意味着,当我把这个类称为

db=  DbHelperClass(this);

加载了libs,使得RAM较低的手机有点慢。

我尝试在oncreate方法中加载lib ....但是出错...

@Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub


    //SQLiteDatabase.loadLibs(cnxt);
    //this.getWritableDatabase(PWD);


    }

这里是错误日志......

08-04 09:24:47.577: E/AndroidRuntime(797): FATAL EXCEPTION: main
08-04 09:24:47.577: E/AndroidRuntime(797): java.lang.UnsatisfiedLinkError: Native method not found: net.sqlcipher.database.SQLiteDatabase.dbopen:(Ljava/lang/String;I)V
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteDatabase.dbopen(Native Method)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1942)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:875)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:907)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:197)
08-04 09:24:47.577: E/AndroidRuntime(797):  at net.sqlcipher.database.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:184)
08-04 09:24:47.577: E/AndroidRuntime(797):  at mmHelper.DbHelperClass.Registered(DbHelperClass.java:391)
08-04 09:24:47.577: E/AndroidRuntime(797):  at com.mobilemoney.logger.StartActivity$1.run(StartActivity.java:40)
08-04 09:24:47.577: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:730)
08-04 09:24:47.577: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
08-04 09:24:47.577: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
08-04 09:24:47.577: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:5103)
08-04 09:24:47.577: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
08-04 09:24:47.577: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:525)
08-04 09:24:47.577: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-04 09:24:47.577: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-04 09:24:47.577: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)

关于如何加载lib一次的任何建议?

罗纳德

1 个答案:

答案 0 :(得分:0)

通过使用Singleton类来解决它....现在超级快!

private Context cnxt;
 private static DbHelperClass sInstance;
 private SQLiteDatabase db = null;
 public static DbHelperClass getInstance(Context context) {

        if (sInstance == null) {
          sInstance = new DbHelperClass(context.getApplicationContext(),DB_NAME,DB_VERSION);
        }
        return sInstance;
      }

    private DbHelperClass(final Context context,final String name,final int version) {
        super(context,DB_NAME,null, DB_VERSION);

        SQLiteDatabase.loadLibs(context);
        DbHelperClass.this.getWritableDatabase(PWD);



    }
    @Override
    public synchronized void close() {
        if (sInstance != null)
            db.close();
    }