Shared Preference Modes getSharedPreferences(String name,int mode) getPreferences(int mode) Context.MODE_PRIVATE (or 0) o created file can be accessed only by the calling application o generally the only preference mode that you should use Context.MODE_WORLD_READABLE(or 1,deprecated in API level 17) o other applications have read access to the created file Context.MODE_WORLD_WRITEABLE (or 2,deprecated in API level 17) o other applications have write access to the created file Context Class Reference:http://developer.android.com/reference/android/content/Context.html
Shared Preference Modes • getSharedPreferences(String name, int mode) • getPreferences(int mode) • Context.MODE_PRIVATE (or 0) o created file can be accessed only by the calling application o generally the only preference mode that you should use • Context.MODE_WORLD_READABLE (or 1, deprecated in API level 17) o other applications have read access to the created file • Context.MODE_WORLD_WRITEABLE (or 2, deprecated in API level 17) o other applications have write access to the created file 11 Context Class Reference: http://developer.android.com/reference/android/content/Context.html
Writing Shared Preferences To write shared preference values: o Call edit()to get a SharedPreferences.Editor. o Add values with editor"put"methods such as putBoolean()and putstring(). o Commit the new values with apply()or commit(). /Store the preferences /We need an Editor object to make preference changes. SharedPreferences settings getSharedPreferences(PREFS_NAME,0); SharedPreferences.Editor editor settings.edit(); editor.putInt("key",value); /Commit the edits! editor.commit(); 12
Writing Shared Preferences • To write shared preference values: o Call edit() to get a SharedPreferences.Editor. o Add values with editor “put” methods such as putBoolean() and putString(). o Commit the new values with apply() or commit(). // Store the preferences // We need an Editor object to make preference changes. SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putInt(“key”, value); // Commit the edits! editor.commit(); 12
Reading Shared Preferences To read shared preference values: o Use SharedPreferences"get"methods such as getBoolean(String key,boolean defValue)and getstring(String key,String defValue). o The"get"methods have two parameters key:the preference key string defValue:a default value to return if the preference is undefined /Restore the preferences /Get the preference with the name specified SharedPreferences settings getSharedPreferences(PREFS_NAME,0); /Read the stored data from the preference int data settings.getInt("key",defaultValue); 13
Reading Shared Preferences • To read shared preference values: o Use SharedPreferences “get” methods such as getBoolean(String key, boolean defValue) and getString(String key, String defValue). o The “get” methods have two parameters ▪ key: the preference key string ▪ defValue: a default value to return if the preference is undefined // Restore the preferences // Get the preference with the name specified SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); // Read the stored data from the preference int data = settings.getInt(“key", defaultValue); 13
Selected Methods for Retrieving Shared Preferences /in interface SharedPreferences boolean getBoolean(String key,boolean defValue) float getFloat(String key,float defValue) int getInt(String key,int defValue) long getLong(String key,long defValue) String getString(String key,String defValue) Set<String>getstringSet(String key,Set<string>defValues) SharedPreferences Class References: http://developer.android.com/reference/android/content/SharedPreferences.html 14
Selected Methods for Retrieving Shared Preferences // in interface SharedPreferences boolean getBoolean(String key, boolean defValue) float getFloat(String key, float defValue) int getInt(String key, int defValue) long getLong(String key, long defValue) String getString(String key, String defValue) Set<String> getStringSet(String key, Set<String> defValues) 14 SharedPreferences Class References: http://developer.android.com/reference/android/content/SharedPreferences.html
Selected Methods for Saving Shared Preferences /in interface SharedPreferences.Editor void apply() boolean commit() SharedPreferences.Editor putBoolean(String key,boolean value) SharedPreferences.Editor putFloat(String key,float value) SharedPreferences.Editor putInt(String key,int value) SharedPreferences.Editor putLong(String key,long value) SharedPreferences.Editor putstring(String key,String value) SharedPreferences.Editor putstringSet(String key, Set<String>values) SharedPreferences.Editor remove(String key) SharedPreferences Class References: http://developer.android.com/reference/android/content/SharedPreferences.html 15
Selected Methods for Saving Shared Preferences // in interface SharedPreferences.Editor void apply() boolean commit() SharedPreferences.Editor putBoolean(String key, boolean value) SharedPreferences.Editor putFloat(String key, float value) SharedPreferences.Editor putInt(String key, int value) SharedPreferences.Editor putLong(String key, long value) SharedPreferences.Editor putString(String key, String value) SharedPreferences.Editor putStringSet(String key, Set<String> values) SharedPreferences.Editor remove(String key) 15 SharedPreferences Class References: http://developer.android.com/reference/android/content/SharedPreferences.html