Chapter 4
Chapter 4
3/25/2024 MAD 1
Explain how to store and retrieving data using mobile application
✓Android provides several options for you to save persistent application data.
✓The solution you choose depends on your specific needs. Such as whether the data
should be private to your application or accessible to other applications (and the
user).
✓ The APIs you'll need to use a database on Android are available in the
android.database.sqlite package.
3/25/2024 MAD 2
Your data storage options are the following:
Shared Preferences
Internal Storage
External Storage
SQLite Databases
• Network Connection
3/25/2024 MAD 3
Android Internal Storage
• We are able to save or read data from the device internal memory. FileInputStream and
FileOutputStream classes are used to read and write data into the file.
▪ When files are stored in internal storage these file can only be accessed by the
application itself not by other applications.
▪ These files in storage exist till the application stays over the device, as you uninstall
associated files get removed automatically.
▪ The files are stored in directory data/data which is followed by the application
package
3/25/2024 name. MAD 4
Android External Storage
• Like internal storage, we are able to save or read data from the device external
memory such as sdcard.
• The FileInputStream and FileOutputStream classes are used to read and write data
into the file.
• <usespermission android:name="android.permission.WRITE_EXTERNAL_STO
RAGE"/>
3/25/2024 MAD 5
SQLite
• SQLite is an open-source relational database i.e. used to perform
database operations on android devices such as storing, manipulating
or retrieving persistent data from the database.
➢ Open source.
➢ Light weight.
➢ No network access.
➢ Standalone database.
3/25/2024 MAD 7
When We Need SQLite?
• If you are developing embedded software for devices like mobile phones,
televisions, home electronic devices, cameras, etc., then sqlite is a good choice.
3/25/2024 MAD 8
SQLite Supporting Data Types:
• TEXT
• INTEGER
• REAL
3/25/2024 MAD 9
SQLite Open Helper:
• When the application runs the first time – this point, we do not have a
database, so here in this class, you can create a database.
3/25/2024 MAD 10
Creating a Database in Android
3/25/2024 MAD 11
SQLiteOpenHelper constructor
• Context – This is the context that will be used to create the database.
(Here you can pass the Activity object.)
3/25/2024 MAD 13
There are two constructors of SQLiteOpenHelper class.
3/25/2024 MAD 14
Methods of SQLiteOpenHelper class
• There are many methods in SQLiteOpenHelper class. Some of them
are as follows:
3/25/2024 MAD 15
SQLiteDatabase class
3/25/2024 MAD 16
Cont.…
3/25/2024 MAD 17
Using Shared Preferences
• The Shared Preferencesclass provides a general framework that allows you to save and
retrieve persistent key-value pairs of primitive data types.
• You can use Shared Preferencesto save any primitive data: booleans, floats, ints, longs,
and strings.
• This data will persist across user sessions (even if your application is killed).
• To get a Shared Preferences object for your application, use one of two methods:
3/25/2024 MAD 18
Cont.…
getPreferences() – Use this if you need only one preferences file for your
Activity. Because this will be the only preferences file for your Activity,
you don’t supply a name.
To write values:
3/25/2024 MAD 19
Using the Internal Storage
✓You can save files directly on the device’s internal storage. By default, files saved to
the internal storage are private to your application and other applications cannot
access them (nor can the user).
✓When the user uninstalls your application, these files are removed.
✓Saving cache files: When the device is low on internal storage space, Android may
delete these cache files to recover space.
✓However, you should not rely on the system to clean up these files for you.
3/25/2024 MAD 20
Cont.…
Other useful methods
✓getFilesDir():Gets the absolute path to the filesystem directory where your internal
files are saved.
✓getDir():Creates (or opens an existing) directory within your internal storage space.
3/25/2024 MAD 21
Accessing files on external storage
✓API Level 8 or greater, use getExternalFilesDir()to open a File that represents the
external storage directory where you should save your files.
✓This method takes a type parameter that specifies the type of subdirectory you
want.
✓/Android/data/<package_name>/files/
3/25/2024 MAD 22
Using Databases
• Any databases you create will be accessible by name to any class in the application, but
not outside the application.
3/25/2024 MAD 23
Database debugging
• The Android SDK includes a sqlite3database tool that allows you to browse table
contents, run.
✓You can use the network (when it’s available) to store and retrieve data on your own
web-based services.
• java.net.*
• android.net.*
3/25/2024 MAD 24
Synchronization and Replication of Mobile Data
✓Data synchronization is a method of establishing consistency among data from a data
source to the target data storage and vice versa.
✓In data synchronization, we have to keep multiple copies of a dataset in coherence with one
another to maintain the data integrity.
✓Data synchronization provides the continuous harmonization of the data over time.
✓ This is the basic fundamental concept used in a wide variety of applications, including file
synchronization and mobile device synchronization.
✓Data synchronization ensures accurate, secure, compliant data and successful team and
customer experiences.
✓Local synchronization involves devices and computers that are next to each other, while
remote synchronization takes place over a mobile network.
✓ Data must always be consistent throughout the data record.
3/25/2024 MAD 25
Cont.…
✓ Data synchronization is required between the mobile devices and their service
provider.
✓ It is also required between the device and personal area computer and nearby wireless
access points (in Wi-Fi connection) and other nearby devices.
✓ It is used to establish consistency among data from a data source to the target data
storage and vice versa.
3/25/2024 MAD 26
Example of Data Synchronization
✓Suppose we have added a new popular ringtone to one of the servers of a mobile
service provider, so here, data synchronization means that all the service provider
servers get identical sets of ringtones.
✓All the devices connected to the server should be updated about the availability of
the new data.
✓The ringtone databases available to all the mobile phones include a copy of the
title of that tone.
3/25/2024 MAD 27
Exploring data synchronization methods
• There are a few types of data synchronization methods. Version control and file
synchronization tools can make changes to more than one copy of a file at a time.
The other two, distributed and mirror, have more specific uses.
✓ File Synchronization: Faster and more error-proof than a manual copy technique,
this method is most used for home backups, external hard drives, or updating
portable data via flash drive.
✓ File synchronization ensures that two or more locations share the same data,
occurs automatically, and prevents duplication of identical files.
3/25/2024 MAD 28
Cont.…
• Version Control: This technique aims to provide synchronizing solutions for files that can be
altered by more than one user at the same time.
• Distributed File Systems: When multiple file versions must be synced at the same time on
different devices, those devices must always be connected for the distributed file system to
work.
• A few of these systems allow devices to disconnect for short periods of time, as long as data
reconciliation is implemented before synchronization.
• Mirror Computing: Mirror computing is used to provide different sources with an exact copy of
a data set.
• Especially useful for backup, mirror computing provides an exact copy to just one other
location — source to target.
3/25/2024 MAD 29
Working with a Content Provider
✓Content Providers are a very important component that serves the purpose of a relational
database to store the data of applications.
✓The role of the content provider in the android system is like a central repository in which
data of the applications are stored, and it facilitates other applications to securely access and
modifies that data based on the user requirements.
✓ Android system allows the content provider to store the application data in several ways.
✓Users can manage to store the application data like images, audio, videos, and personal
contact information by storing them in SQLite Database, in files, or even on a network.
✓In order to share the data, content providers have certain permissions that are used to grant or
restrict the rights to other applications to interfere with the data.
3/25/2024 MAD 30
Cont..
3/25/2024 MAD 31
Content URI
✓Content URI (Uniform Resource Identifier) is the key concept of Content providers. To
access the data from a content provider, URI is used as a query string.
✓Structure of a Content URI: content://authority/optionalPath/optionalID
Details of different parts of Content URI:
✓ content:// – Mandatory part of the URI as it represents that the given URI is a Content
URI.
✓authority – Signifies the name of the content provider like contacts, browser, etc. This
part must be unique for every content provider.
✓optionalPath – Specifies the type of data provided by the content provider. It is
essential as this part helps content providers to support different types of data that are
not related to each other like audio and video files.
✓optionalID – It is a numeric value that is used when there is a need to access a
particular record.
3/25/2024 MAD 32
Four fundamental operations are possible in Content Provider
namely Create, Read, Update, and Delete.
• These operations are often termed as CRUD operations.
3/25/2024 MAD 33
Cont.
✓Working of the Content Provider UI components of android applications like
Activity and Fragments use an object CursorLoader to send query requests to
ContentResolver. UI component of an android application is as follow..
3/25/2024 MAD 34
Creating a Content Provider
Following are the steps which are essential to follow in order to create a Content
Provider:
I. Create a class in the same directory where the that MainActivity file resides and
this class must extend the ContentProvider base class.
As the content provider is created, the android system calls this method
onCreate() immediately to i provider.
3/25/2024 MAD 36
37