0% found this document useful (0 votes)
19 views37 pages

Chapter 4

Chapter Four discusses methods for storing and retrieving data in mobile applications, focusing on Android's various storage options such as Shared Preferences, Internal Storage, External Storage, and SQLite databases. It also covers data synchronization and replication, emphasizing the importance of maintaining data consistency across devices and applications. Additionally, the chapter explains the role of Content Providers in managing application data and outlines the fundamental operations associated with them.

Uploaded by

bekeletamirat931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views37 pages

Chapter 4

Chapter Four discusses methods for storing and retrieving data in mobile applications, focusing on Android's various storage options such as Shared Preferences, Internal Storage, External Storage, and SQLite databases. It also covers data synchronization and replication, emphasizing the importance of maintaining data consistency across devices and applications. Additionally, the chapter explains the role of Content Providers in managing application data and outlines the fundamental operations associated with them.

Uploaded by

bekeletamirat931
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Chapter Four: Storing and Retrieving Data

✓ Explain how to store and retrieving data using mobile application

✓ Describe Synchronization and Replication of Mobile Data

✓ Android storing and retrieving

✓ Understand working with a content provide

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).

✓ How much space your data requires.

✓ 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

• Store private primitive data in key-value pairs.

Internal Storage

• Store private data on the device memory.

External Storage

• Store public data on the shared external storage.

SQLite Databases

• Store structured data in a private database.

• Network Connection

• Store data on the web with your own network server.

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.

➢Important Points about Internal Storage in Android

▪ The stored data in memory is allowed to read and write files.

▪ 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.

• Provide permission for the external storage

• You need to provide the WRITE_EXTERNAL_STORAGE permission.

• <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.

• It is embedded in android by default. So, there is no need to perform


any database setup or administration task.

• SQLiteOpenHelper class provides the functionality to use the SQLite


database.
3/25/2024 MAD 6
SQLite is:
➢ A Structure query base database.

➢ Open source.

➢ Light weight.

➢ No network access.

➢ Standalone database.

➢ It support embedded relational database features.

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.

• It is free and easy to use.

• SQLite is open source, no commercial license required to use.

• SQLite does not require a server to perform operations.

• No extra setup or administration needed, it is embedded with Android.

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.

• When the application is upgraded to a new schema – our database will


still be on the old schema from the older edition version of the app.

• We will have the option to alter or upgrade the database schema to


match the needs of the app.

3/25/2024 MAD 10
Creating a Database in Android

public DatabaseHelper(Context context) {


super(context, DB_NAME, null, DB_VERSION);
}
✓In order to write data to a database, we need to create our database first. To
create a database in SQLite in Android, you need to create a subclass of the
class SQLiteOpenHelper.

✓This class provides the functionality to create a database and calls a


function that can be overridden by the derived class.
.

3/25/2024 MAD 11
SQLiteOpenHelper constructor

• The SQLiteOpenHelper constructor accepts four arguments, which are

• Context – This is the context that will be used to create the database.
(Here you can pass the Activity object.)

• Name – The name of the new database file.

• Factory – A cursor factory (you can usually pass this as null)

• Version – The version of the database. This number is used to identify if


there is an upgrade or downgrade of the database
3/25/2024 MAD 12
Cont.…

✓ on Create(SQLiteDatabase db) :It’s called when there is no database


and the app needs one and to create a new database.

✓ on Upgrade(SQLiteDatabase db, int oldVersion, int newVersion) : It’s


called when the schema version we need does not match the new schema
version of the database.

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

• It contains methods to be performed on sqlite database such as create,


update, delete, select etc.

• Methods of SQLiteDatabase class: There are many methods in


SQLiteDatabase class. Some of them are as follows:

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:

• getSharedPreferences() – Use this if you need multiple preferences files identified by


name, which you specify with the first parameter.

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:

✓ Call edit()to get a SharedPreferences.Editor.

✓ Add values with methods such as putBoolean()and putString().

✓ Commit the new values with commit().

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.

✓deleteFile():Deletes a file saved on the internal storage.

✓fileList():Returns an array of files currently saved by your application.

Using the External Storage

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.

✓API Level 7 or lower, use getExternalStorageDirectory(), to open a File


representing the root of the external storage.

✓You should then write your data in the following directory:

✓/Android/data/<package_name>/files/

3/25/2024 MAD 22
Using Databases

• Android provides full support for SQLite databases.

• Any databases you create will be accessible by name to any class in the application, but
not outside the application.

• The recommended method to create a new SQLite database is to create a subclass of


SQLiteOpenHelper and override the onCreate() method, in which you can execute a
SQLite command to create tables in the database.

3/25/2024 MAD 23
Database debugging
• The Android SDK includes a sqlite3database tool that allows you to browse table
contents, run.

• SQL commands, and perform other useful functions on SQLite databases.

Using a Network Connection

✓You can use the network (when it’s available) to store and retrieve data on your own
web-based services.

✓To do network operations, use classes in the following packages:

• 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 important and required in mobile computing because it checks


the differences between two data containers or data sources and data receivers to restrict
the unnecessary transfer of data that already resides in both data sources.
The following are the reasons why data synchronization is required in Mobile computing:

✓ 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.

✓ PDAs. It is also used in encryption for synchronizing Public Key Servers.

✓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.

1. Create: Operation to create data in a content provider.

2. Read: Used to fetch data from a content provider.

3. Update: To modify existing data.

4. Delete: To remove existing data from the storage.

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.

II. To access the content, define a content provider URI address.

III. Create a database to store the application data.

IV. Implement the six abstract methods of ContentProvider class.

V. Register the content provider in AndroidManifest.xml file using <provider> tag.


3/25/2024 MAD 35
Following are the six abstract methods and their description which
are essential to override as the part of Content Provider class:
Abstract Description
Method
A method that accepts arguments and fetches the data from the desired table.
query() Data is retired
object.
insert() To insert a new row in the database of the content provider. It returns the
content URI of the in
update() This method is used to update the fields of an existing row. It returns the
number of rows upd
delete() This method is used to delete the existing rows. It returns the number of rows
deleted.
This method returns the Multipurpose Internet Mail Extension(MIME) type of
getType() data to the gi URI.

As the content provider is created, the android system calls this method
onCreate() immediately to i provider.
3/25/2024 MAD 36
37

Complied by:Atinkut M.(MSc.,) 3/25/2024

You might also like