Uploaded by ishrat fatima

MAD SharedPref Lecture

advertisement
Mobile App development
SharedPreferences
SharedPreferences
• Android provides many ways of storing data of an application.
• One of this way is called Shared Preferences.
• Shared Preferences allow you to save and retrieve data in the form of
key,value pair.
• A preference file is actually a xml file saved in internal memory of
device.
• "key" : "value"
• "username" : “Ali"
• "password" : "12345678"
Difference between SQLite
• Large amounts of same structured data should be stored in a SQLite
database as databases are designed for this kind of data.
• As the data is structured and managed by the database, it can be
queried to get a sub set of the data which matches certain criteria using
a query language like SQL.
• This makes it possible to search in the data. Of course managing and
searching large sets of data influences the performance so reading data
from a database can be slower than reading data from
SharedPreferences.
• Shared Preferences are suitable in different situations. For example,
when the user’s settings need to be saved
SharedPreferences
• SharedPreferences is a key/value store where you can save a data
under certain key.
• To read the data from the store you have to know the key of the data.
This makes reading the data very easy.
• But as easy as it is to store a small amount of data as difficult it is to
store and read large structured data as you need to define key for
every single data, furthermore you cannot really search within the
data except you have a certain concept for naming the keys.
Cont.
SharedPreferences is application specific, i.e. the data is lost on
performing one of the following options:
• on uninstalling the application
• on clearing the application data (through Settings)
the primary purpose is to store user-specified configuration details,
such as user specific settings, keeping the user logged into the
application.
example
• Music app store all settings in shared preferences, but store artists,
albums, genres, tracks, playlists, etc in a sqlite db
Creating preference file
• The first thing we need to do is to create one shared preferences file
per app we use SharedPreference class.
• When you want to get the values, call the getSharedPreferences()
method.
• Shared Preferences provide modes of storing the data (private mode
and public mode).
• MODE_PRIVATE to be secure.
• SharedPreferences Pref= getSharedPreferences("MySharedPref",MODE_PRIVATE);
Cont..
• SharedPreferences Pref= getSharedPreferences("MySharedPref",MODE_PRIVATE);
Editing in pref file
• SharedPreferences.Editor: Interface used to write(edit) data in the SP
file. Once editing has been done, one must commit() or apply() the
changes made to the file.
• SharedPreferences.OnSharedPreferenceChangeListener(): Called
when a shared preference is changed, added, or removed. This may
be called even if a preference is set to its existing value.
methods of Shared Preferences
1. contains(String key): This method is used to check whether the preferences contain a key value.
2. edit(): This method is used to create a new Editor for these preferences, through which you can make modifications to
the data in the preferences and atomically commit those changes back to the SharedPreferences object.
3. getAll(): This method is used to retrieve all values from the preferences.
4. getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the preferences.
5. getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences.
6. getInt(String key, int defValue): This method is used to retrieve an int value from the preferences.
7. getLong(String key, long defValue): This method is used to retrieve a long value from the preferences.
8. getString(String key, String defValue): This method is used to retrieve a String value from the preferences.
9. getStringSet(String key, Set defValues): This method is used to retrieve a set of String values from the preferences.
example
Retrieving data
Example 2
Cont..
Cont..
Cont..
Download