SharedPreferencesUtil 工具类

SharedPreferencesUtil 工具类

public class SharedPreferencesUtil {

    public final static String SETTING = "SETTING";
    private static SharedPreferences sp;
    private static SharedPreferences.Editor editor;

    public static void init(Context context) {
        sp = context.getSharedPreferences(SETTING, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    //存储
    public static void putInt(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void putString(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    //取值
    public static int getInt(String key, int defValue) {
        int value = sp.getInt(key, defValue);
        return value;
    }

    public static boolean getBoolean(String key, boolean defValue) {
        boolean value = sp.getBoolean(key, defValue);
        return value;
    }

    public static String getString(String key, String defValue) {
        String value = sp.getString(key, defValue);
        return value;
    }

    //移除数据
    public void removeValue(String key) {
        editor.remove(key);
        editor.commit();
    }

    //清空
    public void clear() {
        editor.clear().commit();
    }
}

猜你喜欢

转载自blog.csdn.net/bluesky003/article/details/80078577
今日推荐