GreenDao3.0使用(二)

在上篇文章中简单实现了欢迎页面。
在WelcomeActivity中要判断是否是“首次安装或者刚刚更新到最新版本”需要将之前保存的版本号与当前的版本号进行比较。
1、如果之前保存的版本号为空,说明没有保存过版本号,那么为首次安装。
2、如果之前保存的版本号小于当前的版本号,说明刚刚更新到最新版本。
保存版本号可以使用GreenDao。

GreenDao3.0以上版本的简单使用请参考我之前的文章。

1、新建一个名为VersionInfo的实体类

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class VersionInfo {

    @Property(nameInDb = "VERSIONKEY")
    private String versionkey;
    @Property(nameInDb = "VERSIONVALUE")
    private String versionvalue;

}

Rebuild Project 后代码如下:

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Generated;

@Entity
public class VersionInfo {

    @Property(nameInDb = "VERSIONKEY")
    private String versionkey;
    @Property(nameInDb = "VERSIONVALUE")
    private String versionvalue;
    @Generated(hash = 1506572606)
    public VersionInfo(String versionkey, String versionvalue) {
        this.versionkey = versionkey;
        this.versionvalue = versionvalue;
    }
    @Generated(hash = 2119036008)
    public VersionInfo() {
    }
    public String getVersionkey() {
        return this.versionkey;
    }
    public void setVersionkey(String versionkey) {
        this.versionkey = versionkey;
    }
    public String getVersionvalue() {
        return this.versionvalue;
    }
    public void setVersionvalue(String versionvalue) {
        this.versionvalue = versionvalue;
    }

}

2、新建MainApp继承Application

package com.hongxue.jianwen.app;

import android.app.Application;
import android.content.Context;
import com.hongxue.jianwen.dao.DaoMaster;
import com.hongxue.jianwen.dao.DaoSession;
import org.greenrobot.greendao.database.Database;

public class MainApp extends Application {

    // 官方推荐将获取 DaoMaster 对象的方法放到 Application 层,这样将避免多次创建生成 Session 对象
    private static DaoMaster daoMaster;
    private static DaoSession daoSession;

    public static  DaoMaster getDaoMaster(final Context context){
        if (daoMaster == null) {
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "jianwen-db");
            Database db =  helper.getWritableDb();
            daoMaster = new DaoMaster(db);
            daoSession = daoMaster.newSession();
        }
        return daoMaster;
    }

    /**
     * 取得DaoSession
     */
    public static DaoSession getDaoSession(Context context) {
        if (daoSession == null) {
            if (daoMaster == null) {
                daoMaster = getDaoMaster(context);
            }
            daoSession = daoMaster.newSession();
        }
        return daoSession;
    }

}

WelComeActivity.java

 @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

        AppConfig appConfig = AppConfig.getAppConfig(this);

        String versionValue = appConfig.getVersionValue(AppConfig.VERSION_INFO);//获取版本号信息

        if (StringUtils.isEmpty(versionValue) || (StringUtils.parseInt(versionValue) < BuildConfig.VERSION_CODE)) {//说明首次安装或者刚刚更新到最新版本
            appConfig.setVersionInfo(AppConfig.VERSION_INFO, BuildConfig.VERSION_CODE + "");//保存版本号信息
            initFirstView();
        } else {
            initOtherView();
        }

    }

AppConfig.java

public class AppConfig {

    public final static String VERSION_INFO = "version.info";//

    private Context mContext;
    private static AppConfig appConfig;

    public static AppConfig getAppConfig(Context context) {
        if (appConfig == null) {
            appConfig = new AppConfig();
            appConfig.mContext = context;
        }
        return appConfig;
    }


    public String getVersionValue(String key) {
        VersionInfo versionInfo = DbServiceUtil.getInstance(mContext).getVersionInfo(key);
        if (versionInfo != null) {
            return versionInfo.getVersionvalue();
        }
        return null;
    }

    public void setVersionInfo(String key, String value) {
        DbServiceUtil.getInstance(mContext).setVersionInfo(key, value);
    }

}

DbServiceUtil.java

import android.content.Context;
import com.hongxue.common.utils.L;
import com.hongxue.common.utils.StringUtils;
import com.hongxue.jianwen.app.MainApp;
import com.hongxue.jianwen.bean.VersionInfo;
import com.hongxue.jianwen.dao.DaoSession;
import com.hongxue.jianwen.dao.UserInfoDao;
import com.hongxue.jianwen.dao.VersionInfoDao;
import org.greenrobot.greendao.query.QueryBuilder;

public class DbServiceUtil {

    private static DbServiceUtil instance;
    private DaoSession mDaoSession;
    private VersionInfoDao versionInfoDao;

    private DbServiceUtil() {
    }

    public static DbServiceUtil getInstance(Context context) {
        if (instance == null) {
            instance = new DbServiceUtil();
            instance.mDaoSession = MainApp.getDaoSession(context);
            instance.versionInfoDao = instance.mDaoSession.getVersionInfoDao();

        }
        return instance;
    }

    /**
     * 获取版本号信息
     */
    public VersionInfo getVersionInfo(String key) {
        QueryBuilder<VersionInfo> appStartQueryBuilder = versionInfoDao.queryBuilder();
        appStartQueryBuilder.where(VersionInfoDao.Properties.Versionkey.eq(key));
        return appStartQueryBuilder.unique();
    }

    /**
     * 保存版本号信息
     */
    public void setVersionInfo(String key,String value) {
        VersionInfo versionInfo = new VersionInfo();
        versionInfo.setVersionkey(key);
        versionInfo.setVersionvalue(value);
        versionInfoDao.insertOrReplace(versionInfo);
    }

}
    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
     *
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }
  /**
     * 解析一个字符串为整数;
     */
    public final static int parseInt(String value) {
        if (isInt(value))
            return Integer.parseInt(value);
        return 0;
    }
    /**
     * 检测变量的值是否为一个整型数据;
     */
    public final static boolean isInt(String value) {
        if (isEmpty(value))
            return false;

        try {
            Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return false;
        }

        return true;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值