Android 开发 SharedPreferences数据会话类模板

简单的模板

  

public class SPDataSession {
    private static SPDataSession mSPDataSession;
    private SharedPreferences.Editor mEditor;
    private SharedPreferences mSP;
    private SPDataSession(){
        mEditor = App.context().getSharedPreferences("data",Context.MODE_PRIVATE).edit();
        mSP = App.context().getSharedPreferences("data",Context.MODE_PRIVATE);

    }
    public static SPDataSession I(){
        if (mSPDataSession == null){
            mSPDataSession = new SPDataSession();
        }
        return mSPDataSession;
    }

    /**
     * 保存第一次登入
     */
    public void saveFirst(){
        mEditor.putBoolean("first",true);
        mEditor.apply();
    }

    /**
     * 获取第一次登入记录
     * @return
     */
    public boolean getFirst(){
        return mSP.getBoolean("first",false);
    }

    /**
     * 保存设备识别id
     * @param deviceId
     */
    public void saveDeviceId(String deviceId){
        mEditor.putString("deviceId",deviceId);
        mEditor.apply();
    }

    /**
     * 获取设备识别id
     * @return
     */
    public String getDeviceId(){
        return mSP.getString("deviceId","");

    }

    /**
     * 判断token为空
     * @return true=空 false=不为空
     */
    public boolean isTokenEmpty(){
        if (TextUtils.isEmpty(mSP.getString("token",""))){
            return true;
        }
        return false;
    }

}
原文地址:https://www.cnblogs.com/guanxinjing/p/10314290.html