MrShi
5 天以前 3a154bdb0a5aaa2c0ac3eac95a6ba747068bd454
keyCabinet-android/lib_coremodel/src/main/java/com/doumee/lib_coremodel/util/SpUtil.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,235 @@
package com.doumee.lib_coremodel.util;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.text.TextUtils;
import android.view.WindowManager;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
/**
 * Created by Administrator on 2016/10/14.
 */
public class SpUtil {
    private static final String FILE_NAME = "app_parameter";
    private static SharedPreferences sharedPreferences;
    public static void init(Context context){
        if (sharedPreferences == null) {
            sharedPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        }
    }
    private static SharedPreferences getSharedPreferences() {
        return sharedPreferences;
    }
    public static boolean remoreUser() {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove("user");
        return editor.commit();
    }
    public static <T> T getBean(String key,Class<T> cls) {
        String s = getSharedPreferences().getString(key, "");
        if (!TextUtils.isEmpty(s)) {
            return new Gson().fromJson(s, cls);
        }
        return null;
    }
    public static boolean saveBean(String key,Object labour) {
        String s = new Gson().toJson(labour);
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, s);
        return editor.commit();
    }
    public static String getString(String key){
        return getSharedPreferences().getString(key, "");
    }
    /**
     * èŽ·å–String
     *
     * @param key
     * @param defValue
     * @return
     */
    public static String getString(String key, String defValue) {
        return getSharedPreferences().getString(key, defValue);
    }
    public static int getInt(String key, int defValue){
        return getSharedPreferences().getInt(key, defValue);
    }
    public static void setInt(String key, int defValue){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putInt(key, defValue);
        editor.commit();
    }
    /**
     * æ·»åŠ String
     *
     * @param key
     * @param value
     */
    public static void setString(String key, String value) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }
    public static void saveString(String key, String value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, value);
        editor.commit();
    }
    public static void saveInt(String key, int value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putInt(key, value);
        editor.commit();
    }
    public static int getInt(String key){
        return getSharedPreferences().getInt(key, 0);
    }
    public static void saveLong(String key, Long value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putLong(key, value);
        editor.commit();
    }
    public static Long getLong(String key){
        return getSharedPreferences().getLong(key, 0L);
    }
    public static boolean getBoolean(String key){
        return getSharedPreferences().getBoolean(key, false);
    }
    public static void saveBoolean(String key, boolean value){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, value);
        editor.commit();
    }
    public static boolean remoreString(String key) {
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove(key);
        return editor.commit();
    }
    public static boolean haveKey(String key){
        return getSharedPreferences().contains(key);
    }
    //是否是游客登陆
    public static boolean isVisitor(){
        return getSharedPreferences().getBoolean("isVisitor",false);
    }
    //是否是游客登陆
    public static void saveVisitor(boolean is){
        SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean("isVisitor", is);
        editor.commit();
    }
    /**
     * å±å¹•宽高
     */
    private static  int WIDTH=0;
    private static  int HEIGHT=0;
    public static int getWIDTH(){
        if(WIDTH==0){
            WIDTH= getInt("WIDTH",0);
        }
        return WIDTH;
    }
    public static void setWIDTH(int width){
        if(width!=0){
            saveInt("WIDTH",width);
            WIDTH=width;
        }
    }
    public static int getHEIGHT(){
        if(HEIGHT==0){
            HEIGHT= getInt("HEIGHT",0);
        }
        return HEIGHT;
    }
    public static void setHEIGHT(int height){
        if(height!=0){
            saveInt("HEIGHT",height);
            HEIGHT=height;
        }
    }
    public static void initWH(Activity activity){
        WindowManager wm = activity.getWindowManager();
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);
        setWIDTH(point.x);
        setHEIGHT(point.y);
    }
    public static List<SPBean> getMatchingStrings(String pattern) {
        List<String> keys = new ArrayList<>(sharedPreferences.getAll().keySet());
        List<SPBean> beans = new ArrayList<>();
        for (String key : keys) {
            if (key.contains(pattern)) {
                beans.add(new SPBean(key,getString(key)));
            }
        }
        return beans;
    }
    public static class SPBean{
        private String key;
        private String value;
        private int pos;
        public SPBean(String key, String value) {
            this.key = key;
            this.value = value;
        }
        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        public int getPos() {
            return pos;
        }
        public void setPos(int pos) {
            this.pos = pos;
        }
    }
}