public class SPUtils {
public static String PREFERENCE_NAME = "config";
public static void clear(Context context) {
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return;
}
SharedPreferences.Editor editor = settings.edit();
editor.clear();
editor.commit();
}
/**
* put string preferences
*
* @param context
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putString(Context context, String key, String value) {
if (context == null) {
return false;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return false;
}
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* put string preferences
*
* @param context
* @param name The name of the preference file
* @param key The name of the preference to modify
* @param value The new value for the preference
* @return True if the new values were successfully written to persistent storage.
*/
public static boolean putString(Context context, String name, String key, String value) {
if (context == null || name == null) {
return false;
}
SharedPreferences settings = context.getSharedPreferences(name, Context.MODE_PRIVATE);
if (settings == null) {
return false;
}
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value);
return editor.commit();
}
/**
* get string preferences
*
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this name that is not a string
* @see #getString(Context, String, String)
*/
public static String getString(Context context, String key) {
return getString(context, key, null);
}
/**
* get string preferences
*
* @param context
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a string
*/
public static String getString(Context context, String key, String defaultValue) {
if (context == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getString(key, defaultValue);
}
/**
* get string preferences
*
* @param context
* @param name The name of the preference file
* @param key The name of the preference to retrieve
* @param defaultValue Value to return if this preference does not exist
* @return The prefer
Android工具类之SharedPreferences
最新推荐文章于 2024-08-09 22:40:08 发布
本文详细探讨了Android中的SharedPreferences,它是Android存储轻量级数据的重要工具。内容涵盖了SharedPreferences的基本使用、读写操作、文件位置、并发控制以及在实际开发中的最佳实践。
摘要由CSDN通过智能技术生成