Sharedpreferences 简单封装

简单粗暴封装 Sharedpreferences
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesUtils {
    private SharedPreferences sp;
    private Context mContext;


    private SharedPreferences getShared(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("config", context.MODE_PRIVATE);

        }
        return sp;


    }

    public void putInt(Context context, String key, int value) {
        getShared(context).edit().putInt(key, value).commit();

    }

    public int getInt(Context context, String key, int defValue) {
        return getShared(context).getInt(key, defValue);
    }

    public void putString(Context context, String key, String value) {
        getShared(context).edit().putString(key, value).commit();
    }

    public String getString(Context context, String key, String defValue) {
        return getShared(context).getString(key, defValue);
    }

    public void putBoolean(Context context, String key, Boolean value) {
        getShared(context).edit().putBoolean(key, value).commit();
    }

    public Boolean getBoolean(Context context, String key, Boolean defValue) {
        return getShared(context).getBoolean(key, defValue);
    }

    public void putFloat(Context context, String key, Float value) {
        getShared(context).edit().putFloat(key, value).commit();
    }

    public Float getFloat(Context context, String key, Float defValue) {
        return getShared(context).getFloat(key, defValue);
    }

    public void putLong(Context context, String key, Long value) {
        getShared(context).edit().putLong(key, value).commit();
    }

    public Float getLong(Context context, String key, Long defValue) {
        return getShared(context).getFloat(key, defValue);
    }
}

 

猜你喜欢

转载自blog.csdn.net/m0_37143081/article/details/80276538