Mobile Application Development (MAD)
GTU # 3170726
Database
Connectivity
Prof. Mehul D Bhundiya
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected] 9428231065
Storages in Android
In Android, there are 2 types of storages are available in android.
Data storage (Databases, SQLite, Realm, etc..).
File storage (SD Card, Phone Storage).
The system provides several options for you to save your app data.
App-specific storage: Store files that are only used for your application, either in dedicated directories
within an internal storage or different dedicated directories within external storage.
Shared Storage: Store files that your app intends to share with other apps, including media, documents,
and other files.
Preferences: Store private, primitive data in key-value pairs inside root directories in xml format.
Databases: Store structured data in a private database using the Room persistence library.
When storing sensitive data that shouldn't be accessible from any other app use internal
storage, preferences, or a database.
Internal storage has the added benefit of the data being hidden from users.
Prof. Mehul Bhundiya #3170726(MAD) Unit 1 – Android OS 2
Shared Preferences
Shared Preferences can store and retrieve small amounts of data in key/value pairs.
It can store String, int, float, Boolean in an XML file inside the app.
It stores data until you clear the preference or uninstall the app from device.
In order to use shared preferences, you have to call a method getSharedPreferences().
SharedPreferences sharedpreferences = getSharedPreferences(“Settings”, Context.MODE_PRIVATE);
The string Settings is the name of the preference file you wish to access If it does not exist, it
will be created.
The mode value designates the default behavior which allow read/write access to only to the
application
Apart from private there are other modes available
MODE_PUBLIC: will make the file public which could be accessible by other applications on the device.
MODE_PRIVATE: keeps the files private and secures the user’s data.
MODE_APPEND: it is used while reading the data from the SP file.
Prof. Mehul Bhundiya #3170726(MAD) Unit 1 – Android OS 3
Shared Preferences
If we want to use common preference file and don't wish to specify a file name, we can use
default shared preferences.
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Using this way will default the preference file to be stored as
/data/data/com.package.name/shared_prefs/com.package.name_preferences.xml
We can write the data inside shared preference file using SharedPreferences.Editor.
Once editing has been done, one must commit() or apply() the changes made to the file.
Prof. Mehul Bhundiya #3170726(MAD) Unit 1 – Android OS 4
Shared Preferences
There are difference methods available of shared preferences:
contains(String key): This method is used to check whether the preferences contain a preference.
edit(): This method is used to create a new Editor for these preferences.
getAll(): This method is used to retrieve all values from the preferences.
getBoolean(String key, boolean defValue): This method is used to retrieve a boolean value from the
preferences.
getFloat(String key, float defValue): This method is used to retrieve a float value from the preferences.
getInt(String key, int defValue): This method is used to retrieve an int value from the preferences.
getLong(String key, long defValue): This method is used to retrieve a long value from the preferences.
getString(String key, String defValue): This method is used to retrieve a String value from the
preferences.
getStringSet(String key, Set defValues): This method is used to retrieve a set of String values from the
preferences.
Prof. Mehul Bhundiya #3170726(MAD) Unit 1 – Android OS 5
Mobile Application Development (MAD)
GTU # 3170726
Thank You
Prof. Mehul D Bhundiya
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
[email protected] +91-9428231065