定义三张数据库
public class CleanMarsterDatabase {
public static final String DATABASE_NAME = "cleanmarter.db";
public static final int DATABASE_VERSION = 1;
/**
* 卸载残留垃圾清理
*/
public static class Uninstall {
public final static String TABLE_NAME = "apk_uninstall";
public static final String KEY_PACKNAME = "packagename";
}
/**
* 缓存垃圾
*/
public static class Cache {
public final static String TABLE_NAME = "cache";
public static final String KEY_PACKNAME = "packagename";
public static final String KEY_CACHE_SIZE = "cache_size";
public static final String KEY_MIN_CACHE_SIZE = "min_cache_size";
public static final String KEY_LAST_TIME = "last_time";
}
/**
* 静默卸载
*/
public static class PackageSize {
public final static String TABLE_NAME = "save_uninstall_info";
public static final String KEY_PACKNAME = "packagename";
public static final String KEY_LAST_TIME = "last_time";
public static final String KEY_PACKAGE_SIZE = "package_size";
}
单例模式数据库
典型单例模式
private static CleanMarsterHelp mInstance;
public static CleanMarsterHelp getInstance(Context mContext) {
if (mInstance == null) {
synchronized (CleanMarsterHelp.class) {
mInstance = new CleanMarsterHelp(mContext);
}
}
return mInstance;
}
比较完整示例
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class CleanMarsterHelp extends SQLiteOpenHelper {
private static CleanMarsterHelp mInstance;
public static CleanMarsterHelp getInstance(Context mContext) {
if (mInstance == null) {
synchronized (CleanMarsterHelp.class) {
mInstance = new CleanMarsterHelp(mContext);
}
}
return mInstance;
}
public CleanMarsterHelp(Context mContext) {
super(mContext, CleanMarsterDatabase.DATABASE_NAME, null, CleanMarsterDatabase.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + CleanMarsterDatabase.Uninstall.TABLE_NAME
+ "("
+ CleanMarsterDatabase.Uninstall.KEY_PACKNAME + " TEXT PRIMARY KEY"
+ ");");
db.execSQL("create table " + CleanMarsterDatabase.PackageSize.TABLE_NAME
+ "("
+ CleanMarsterDatabase.PackageSize.KEY_PACKNAME + " TEXT PRIMARY KEY,"
+ CleanMarsterDatabase.PackageSize.KEY_PACKAGE_SIZE + " integer DEFAULT 0,"
+ CleanMarsterDatabase.PackageSize.KEY_LAST_TIME + " integer DEFAULT 0"
+ ");");
db.execSQL("create table " + CleanMarsterDatabase.Cache.TABLE_NAME
+ "("
+ CleanMarsterDatabase.Cache.KEY_PACKNAME + " TEXT PRIMARY KEY,"
+ CleanMarsterDatabase.Cache.KEY_CACHE_SIZE + " integer DEFAULT 12288,"
+ CleanMarsterDatabase.Cache.KEY_MIN_CACHE_SIZE + " integer DEFAULT 12288,"
+ CleanMarsterDatabase.Cache.KEY_LAST_TIME + " integer DEFAULT 0"
+ ");");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS clean_master");
onCreate(db);
}
}
使用单例模式
建立一个BaseDao的基类,便于子类获得相同的实例及其共同方法
import android.content.Context;
public class BaseDao {
private CleanMarsterHelp mCleanMarsterHelp;
public CleanMarsterHelp openDB(Context mContext) {
if (null == mCleanMarsterHelp) {
mCleanMarsterHelp = CleanMarsterHelp.getInstance(mContext);
}
return mCleanMarsterHelp;
}
public void closeDB() {
if (mCleanMarsterHelp != null) {
mCleanMarsterHelp.getWritableDatabase().close();
}
}
}
子类调用单例 openDb()
子类1
注意这里的openDb()使用单例获取实例
import java.util.HashMap;
import java.util.List;
import com.cleanmaster.domain.CacheInfo;
import com.cleanmaster.utils.CleanMasterUtil;
import com.cleanmaster.utils.ConstantUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
public class CacheDao extends BaseDao {
private static final String TAG = CacheDao.class.getSimpleName();
private Context mContext;
public CacheDao(Context mContext) {
this.mContext = mContext;
}
public void insertCacheSize(Context context, String packageName, Long size) {
ContentValues mContentValues = new ContentValues();
mContentValues.put(CleanMarsterDatabase.Cache.KEY_PACKNAME, packageName);
mContentValues.put(CleanMarsterDatabase.Cache.KEY_CACHE_SIZE, size);
mContentValues.put(CleanMarsterDatabase.Cache.KEY_LAST_TIME, System.currentTimeMillis());
openDB(mContext).getWritableDatabase().insert(CleanMarsterDatabase.Cache.TABLE_NAME, null, mContentValues);
if (ConstantUtil.DEBUG)
Log.i(TAG, "shz insertCacheDb packageName = " + packageName);
}
子类2
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.cleanmaster.domain.UninstallInfo;
import com.cleanmaster.utils.CleanMasterUtil;
import com.cleanmaster.utils.ConstantUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
public class SilentUninstallDao extends BaseDao {
private static final String TAG = SilentUninstallDao.class.getSimpleName();
private Context mContext;
public SilentUninstallDao(Context mContext) {
this.mContext = mContext;
}
public void insertApkInstall(Context context, String packageName, Long size) {
ContentValues mContentValues = new ContentValues();
mContentValues.put(CleanMarsterDatabase.PackageSize.KEY_PACKNAME, packageName);
mContentValues.put(CleanMarsterDatabase.PackageSize.KEY_PACKAGE_SIZE, size);
mContentValues.put(CleanMarsterDatabase.PackageSize.KEY_LAST_TIME, System.currentTimeMillis());
openDB(mContext).getWritableDatabase().insert(CleanMarsterDatabase.PackageSize.TABLE_NAME, null, mContentValues);
if (ConstantUtil.DEBUG)
Log.i(TAG, "shz insertApkInstallDb packageName = " + packageName);
}
子类3
import java.util.ArrayList;
import java.util.List;
import com.cleanmaster.utils.ConstantUtil;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
public class UninstallResidueDao extends BaseDao {
private static final String TAG = UninstallResidueDao.class.getSimpleName();
private Context mContext;
public UninstallResidueDao(Context mContext) {
this.mContext = mContext;
}
public void insertSaveUninstallInfoDb(Context context, String packageName) {
ContentValues mContentValues = new ContentValues();
mContentValues.put(CleanMarsterDatabase.Uninstall.KEY_PACKNAME, packageName);
openDB(mContext).getWritableDatabase().insert(CleanMarsterDatabase.Uninstall.TABLE_NAME, null, mContentValues);
if (ConstantUtil.DEBUG)
Log.i(TAG, "shz insertSaveUninstallInfoDb packageName = " + packageName);
}