SharedPerferences 工具类

来源:互联网 发布:苹果电脑装机软件 编辑:程序博客网 时间:2024/05/18 02:11

给出一段代码,供大家参考

public class PreferenceUtils {private final static String NAME = "projectXXX";private static SharedPreferences preferences;private static SharedPreferences getPreferences(Context context) {        if (preferences == null) {            preferences = context.getSharedPreferences(NAME,                    Context.MODE_PRIVATE);        }        return preferences;    }    public static boolean getBoolean(Context context, String key) {        return getBoolean(context, key, false);    }    public static boolean getBoolean(Context context, String key,            boolean defValue) {        SharedPreferences preferences = getPreferences(context);        return preferences.getBoolean(key, defValue);    }    public static void setBoolean(Context context, String key, boolean value) {        SharedPreferences preferences = getPreferences(context);        Editor editor = preferences.edit();        editor.putBoolean(key, value);        editor.commit();    }    public static String getString(Context context, String key) {        return getString(context, key, null);    }    public static String getString(Context context, String key, String defValue) {        SharedPreferences preferences = getPreferences(context);        return preferences.getString(key, defValue);    }    public static void setString(Context context, String key, String value) {        SharedPreferences preferences = getPreferences(context);        Editor editor = preferences.edit();        editor.putString(key, value);        editor.commit();    }    public static int getInt(Context context, String key) {        return getInt(context, key, -1);    }    public static int getInt(Context context, String key, int defValue) {        SharedPreferences preferences = getPreferences(context);        return preferences.getInt(key, defValue);    }    public static void setInt(Context context, String key, int value) {        SharedPreferences preferences = getPreferences(context);        Editor editor = preferences.edit();        editor.putInt(key, value);        editor.commit();    }}