单例模式数据库

定义三张数据库

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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值