Professional Documents
Culture Documents
MAD Unit-4
MAD Unit-4
MAD Unit-4
GTU # 3161612
Database
Connectivity
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.
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.
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());