1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- package me.yoqi.android.xunfeitts.utils;
- import android.content.Context;
- import android.content.SharedPreferences;
- public class SpUtil {
- private static final String SP_NAME = "config";
- private static SharedPreferences sp;
- public static void saveBoolean(Context context, String key, boolean value) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- sp.edit().putBoolean(key, value).apply();
- }
- public static boolean getBoolean(Context context, String key,
- boolean defValue) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- return sp.getBoolean(key, defValue);
- }
- public static void saveInt(Context context, String key, int value) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- sp.edit().putInt(key, value).apply();
- }
- public static int getInt(Context context, String key, int defValue) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- return sp.getInt(key, defValue);
- }
- public static String getString(Context context, String key, String defValue) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- return sp.getString(key,defValue);
- }
- public static void saveString(Context context, String key, String value) {
- if (sp == null) {
- sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
- }
- sp.edit().putString(key, value).apply();
- }
- }
|