@LevelUpApi(contract=INTERNAL) @ThreadSafe public final class SharedPreferencesUtil extends Object
SharedPreferences
.
Preference keys must be unique, regardless of the type of the preference. A simple technique to
ensure uniqueness is to prefix the key with a class name:
private static final String USERNAME_PREFERENCE = MyThing.class.getName() + ".string_username";
Modifier and Type | Method and Description |
---|---|
static void |
delete(Context context,
String key)
Deletes the given key.
|
static void |
deleteAllPreferences(Context context)
Deletes all shared preferences that were set by
SharedPreferencesUtil . |
static boolean |
getBoolean(Context context,
String key,
boolean defaultValue)
Gets a
boolean for a given key. |
static long |
getLong(Context context,
String key,
long defaultValue)
Gets a
long for a given key. |
static String |
getString(Context context,
String key,
String defaultValue)
Gets a
String for a given key. |
static boolean |
hasKey(Context context,
String key)
Checks if a particular preference has been stored.
|
static void |
putBoolean(Context context,
String key,
boolean value)
Puts a
boolean for a given key. |
static void |
putLong(Context context,
String key,
long value)
Puts a
long for a given key. |
static void |
putString(Context context,
String key,
String value)
Puts a
String for a given key. |
@SlowOperation public static boolean getBoolean(Context context, String key, boolean defaultValue)
boolean
for a given key.context
- Application context.key
- to perform preference lookup.defaultValue
- Default value to return if no value is found in the preferences.boolean
representing the stored preference value. If the preference didn't
exist, it returns defaultValue
.@SlowOperation public static long getLong(Context context, String key, long defaultValue)
long
for a given key.context
- Application context.key
- to perform preference lookup.defaultValue
- Default value to return if no value is found in the preferences.long
representing the stored preference value. If the preference didn't
exist, it returns defaultValue
.@SlowOperation public static String getString(Context context, String key, String defaultValue)
String
for a given key.context
- Application context.key
- to perform preference lookup.defaultValue
- Default value to return if no value is found in the preferences.String
representing the stored preference value. If the preference didn't
exist, it returns defaultValue
.@SlowOperation public static boolean hasKey(Context context, String key)
context
- Application context.key
- Key to look for in the preferences.public static void putBoolean(Context context, String key, boolean value)
boolean
for a given key.context
- Application context.key
- to perform preference lookup.value
- the String
value to store.public static void putLong(Context context, String key, long value)
long
for a given key.context
- Application context.key
- to perform preference lookup.value
- the long
value to store.@SlowOperation public static void putString(Context context, String key, String value)
String
for a given key.context
- Application context.key
- to perform preference lookup.value
- the String
value to store. Supplying null
as the value is
equivalent to calling delete(android.content.Context, java.lang.String)
with this key.@SlowOperation public static void delete(Context context, String key)
context
- Application context.key
- to perform preference lookup.@SlowOperation public static void deleteAllPreferences(Context context)
SharedPreferencesUtil
.context
- Application context.