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 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 name, String key, String defaultValue) {
if (context == null || name == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(name, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getString(key, defaultValue);
}
/**
* put int 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 putInt(Context context, String key, int 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.putInt(key, value);
return editor.commit();
}
/**
* put int 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 putInt(Context context, String name, String key, int 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.putInt(key, value);
return editor.commit();
}
/**
* get int preferences
*
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this name that is not a int
* @see #getInt(Context, String, int)
*/
public static int getInt(Context context, String key) {
return getInt(context, key, -1);
}
/**
* get int 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 int
*/
public static int getInt(Context context, String key, int defaultValue) {
if (context == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getInt(key, defaultValue);
}
/**
* put long 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 putLong(Context context, String key, long 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.putLong(key, value);
return editor.commit();
}
/**
* put long preferences
*
* @param context
* @param fialeName 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 putLong(Context context, String fialeName, String key, long value) {
if (context == null || fialeName == null) {
return false;
}
SharedPreferences settings = context.getSharedPreferences(fialeName, Context.MODE_PRIVATE);
if (settings == null) {
return false;
}
SharedPreferences.Editor editor = settings.edit();
editor.putLong(key, value);
return editor.commit();
}
/**
* get long preferences
*
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this name that is not a long
* @see #getLong(Context, String, long)
*/
public static long getLong(Context context, String key) {
return getLong(context, key, -1);
}
/**
* get long 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 long
*/
public static long getLong(Context context, String key, long defaultValue) {
if (context == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getLong(key, defaultValue);
}
/**
* get long 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 preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a long
*/
public static long getLong(Context context, String name, String key, long defaultValue) {
if (context == null || name == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(name, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getLong(key, defaultValue);
}
/**
* put float 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 putFloat(Context context, String key, float 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.putFloat(key, value);
return editor.commit();
}
/**
* put float 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 putFloat(Context context, String name, String key, float 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.putFloat(key, value);
return editor.commit();
}
/**
* get float preferences
*
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this name that is not a float
* @see #getFloat(Context, String, float)
*/
public static float getFloat(Context context, String key) {
return getFloat(context, key, -1);
}
/**
* get float 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 float
*/
public static float getFloat(Context context, String key, float defaultValue) {
if (context == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getFloat(key, defaultValue);
}
/**
* get float 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 preference value if it exists, or defValue. Throws ClassCastException if there is a preference with this name that is not a float
*/
public static float getFloat(Context context, String name, String key, float defaultValue) {
if (context == null || name == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(name, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getFloat(key, defaultValue);
}
/**
* put boolean 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 putBoolean(Context context, String key, boolean 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.putBoolean(key, value);
return editor.commit();
}
/**
* put boolean 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 putBoolean(Context context, String name, String key, boolean 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.putBoolean(key, value);
return editor.commit();
}
/**
* get boolean preferences, default is false
*
* @param context
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this name that is not a boolean
* @see #getBoolean(Context, String, boolean)
*/
public static boolean getBoolean(Context context, String key) {
return getBoolean(context, key, false);
}
/**
* get boolean preferences, default is false
*
* @param context
* @param name The name of the preference file
* @param key The name of the preference to retrieve
* @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this name that is not a boolean
* @see #getBoolean(Context, String, boolean)
*/
public static boolean getBoolean(Context context, String name, String key, boolean defaultValue) {
if (context == null || name == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(name, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getBoolean(key, defaultValue);
}
/**
* get boolean 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 boolean
*/
public static boolean getBoolean(Context context, String key, boolean defaultValue) {
if (context == null) {
return defaultValue;
}
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
if (settings == null) {
return defaultValue;
}
return settings.getBoolean(key, defaultValue);
}
}