更新 CIMCacheToolkit.java

This commit is contained in:
远方夕阳 2016-12-22 14:52:01 +08:00
parent 0b1c021af2
commit 98b9298e60

View File

@ -37,15 +37,17 @@ class CIMCacheToolkit extends SQLiteOpenHelper {
public static final String KEY_CIM_SERVIER_PORT = "KEY_CIM_SERVIER_PORT"; public static final String KEY_CIM_SERVIER_PORT = "KEY_CIM_SERVIER_PORT";
public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE"; public static final String KEY_CIM_CONNECTION_STATE = "KEY_CIM_CONNECTION_STATE";
private static CIMCacheToolkit toolkit;
static CIMCacheToolkit toolkit; private SQLiteDatabase mSQLiteDatabase;
public static CIMCacheToolkit getInstance(Context context){ public synchronized static CIMCacheToolkit getInstance(Context context){
if (toolkit==null){ if (toolkit==null){
toolkit = new CIMCacheToolkit(context); toolkit = new CIMCacheToolkit(context);
} }
return toolkit; return toolkit;
} }
public CIMCacheToolkit(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
public CIMCacheToolkit(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version); super(context, name, factory, version);
} }
@ -55,42 +57,30 @@ class CIMCacheToolkit extends SQLiteOpenHelper {
public void remove(String key) public synchronized void remove(String key)
{ {
SQLiteDatabase database = toolkit.getWritableDatabase(); getSQLiteDatabase().execSQL(DELETE_SQL,new String[]{key});
database.execSQL(DELETE_SQL,new String[]{key});
database.close();
toolkit.close();
toolkit = null;
} }
public void putString(String key,String value) public synchronized void putString(String key,String value)
{ {
SQLiteDatabase database = toolkit.getWritableDatabase(); getSQLiteDatabase().execSQL(DELETE_SQL,new String[]{key});
database.execSQL(DELETE_SQL,new String[]{key}); getSQLiteDatabase().execSQL(SAVE_SQL, new String[]{key, value});
database.execSQL(SAVE_SQL, new String[]{key, value});
database.close();
toolkit.close();
toolkit = null;
} }
public String getString(String key) public synchronized String getString(String key)
{ {
String value = null; String value = null;
SQLiteDatabase database = toolkit.getWritableDatabase(); Cursor cursor = getSQLiteDatabase().rawQuery(QUERY_SQL, new String[]{key});
Cursor cursor = database.rawQuery(QUERY_SQL, new String[]{key});
if (cursor!=null&&cursor.moveToFirst()) if (cursor!=null&&cursor.moveToFirst())
{ {
value = cursor.getString(0); value = cursor.getString(0);
cursor.close();
} }
cursor.close();
database.close();
toolkit.close();
toolkit = null;
return value; return value;
} }
@ -123,6 +113,27 @@ class CIMCacheToolkit extends SQLiteOpenHelper {
database.execSQL(TABLE_SQL); database.execSQL(TABLE_SQL);
} }
public static synchronized void destroy(){
if (toolkit!=null){
try{toolkit.mSQLiteDatabase.close();}catch(Exception e){}
try{toolkit.close();}catch(Exception e){}
}
}
private SQLiteDatabase getSQLiteDatabase(){
if(mSQLiteDatabase!=null){
return mSQLiteDatabase;
}else
{
mSQLiteDatabase = getWritableDatabase();
}
return mSQLiteDatabase;
}
@Override @Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {