android一个SharedPreferencesUtils,基于SharedPreferences的工具类

场景:安卓开发中,本地经常保存一些轻量级的数据,用到sp,每用到一个就写一次代码太亢余了,就出了工具类简化代码,供使用

public class SharedPreferencesUtils {
    public static final String TAG = "SharedPreferencesUtils";
    private static final String PROJECTION = BsConstants.PREFERENCE_NAME;

    public static boolean getBoolean(Context context, String key, boolean defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        return preferences.getBoolean(key, defaultValue);
    }

    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putBoolean(key, value);
        commit(editor);

    }

    public static int getInt(Context context, String key, int defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        return preferences.getInt(key, defaultValue);
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putInt(key, value);
        commit(editor);
    }

    public static String getString(Context context, String key, String defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        return preferences.getString(key, defaultValue);
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putString(key, value);
        commitInMain(editor);
    }

    public static float getFloat(Context context, String key, float defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        return preferences.getFloat(key, defaultValue);
    }

    public static void saveFloat(Context context, String key, float value) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putFloat(key, value);
        commit(editor);
    }

    public static long getLong(Context context, String key, long defaultValue) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        return preferences.getLong(key, defaultValue);
    }

    public static void saveLong(Context context, String key, long value) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putLong(key, value);
        commit(editor);
    }

    public static <T> ArrayList<T> getArray(Context context, String key, Class<T> cls) {
        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);

        ArrayList<T> datalist = new ArrayList<T>();
        String strJson = preferences.getString(key, null);
        if (null == strJson) {
            return datalist;
        }
        Log.d(TAG, "getDataList, json:" + strJson);
        try {
            Gson gson = new Gson();
            JsonArray array = new JsonParser().parse(strJson).getAsJsonArray();
            for (JsonElement jsonElement : array) {
                datalist.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            Log.e(TAG, "Exception : " + e.getMessage());
        }
        return datalist;
    }

    public static <T> void saveArray(Context context, String key, List<T> datalist) {
        Gson gson = new Gson();
        String strJson = gson.toJson(datalist);

        SharedPreferences preferences = context.getSharedPreferences(PROJECTION, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= preferences.edit();
        editor.putString(key, strJson);
        commit(editor);
    }

    public static void commit(final SharedPreferences.Editor editor) {
        ThreadPoolUtils.executeSingle(new Runnable() {
            @Override
            public void run() {
                editor.commit();
            }
        });
    }

    //if do commit() in single thread, when we read sp imediately, we cannot get right value.
    public static void commitInMain(final SharedPreferences.Editor editor) {
        editor.commit();
    }

    public static final String PREF_URI = "content://" + BsConstants.REMOTE_PROVIDER_AUTHORITIES;
    public static long getPreferenceLong(Context context, String key, long defValue) {
        Bundle extras = new Bundle();
        extras.putInt(BsConstants.PREF_TYPE_STR, BsConstants.PREF_TYPE_LONG);
        extras.putString(BsConstants.PREF_KEY_STR, key);
        extras.putLong(BsConstants.PREF_DEFAULT_STR, defValue);
        extras = callPreference(context, BsConstants.PREF_GET_STR, extras);
        return extras == null ? defValue : extras.getLong(key, defValue);
    }

    private static Bundle callPreference(Context context, String arg, Bundle extras) {
        ContentResolver PREF_RESOLVER = context.getContentResolver();

        Uri uri = Uri.parse(PREF_URI);
        return PREF_RESOLVER.call(uri, BsConstants.REMOTE_METHOD_NAME_CALLPREFERENCE, arg,
                extras);
    }
}

//在需要保存数据的地方保存

SharedPreferencesUtils.saveBoolean(this,"isEnter_FccCalibration",false);

//在需要取出数据的地方取出

SharedPreferencesUtils.getBoolean(this,"isEnter_FccCalibration",false);

就是以上的简单使用,仅供参考了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值