Shared Preference modes getsharedPreferences(String name, int mode) getPreferences (int mode) Context MODE PRIVATE (or o 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 ContextClassReferencehttp://developer.android.com/referencelandroid/content/context.html 11
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 edito to get a SharedPreferences Editor o Add values with editor" put methods such as putBoolean(ar putstringo o Commit the new values with apply( or commit // Store the preferences / We need an Editor object to make preference changes SharedPreferences settings get SharedPreferences (PreFs NAME, 0); SharedPreferences Editor editor= settings. edito; 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 get sharedPreferences(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 devalue) Iong getLong(string key, long devalue) String getstring (string key, String defvalue) Set<string> getstring Set(String key, Set<string> defvalues) SharedPreferences class references http://developer.android.com/reference/android/content/sharedpreferences.htm 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.androidcom/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