0% found this document useful (0 votes)
7 views5 pages

Storing and Retrieving Data

The document discusses various methods for storing and retrieving data in Android, including SharedPreferences for simple key/value pairs, SQLite for local relational databases, and ContentProviders for sharing data between applications. It explains how to create sample applications to demonstrate these concepts and highlights the importance of permissions and context in accessing stored data. Additionally, it introduces the Cursor object used for processing SQLite results and accessing data through ContentProviders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views5 pages

Storing and Retrieving Data

The document discusses various methods for storing and retrieving data in Android, including SharedPreferences for simple key/value pairs, SQLite for local relational databases, and ContentProviders for sharing data between applications. It explains how to create sample applications to demonstrate these concepts and highlights the importance of permissions and context in accessing stored data. Additionally, it introduces the Cursor object used for processing SQLite results and accessing data through ContentProviders.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

7.

Storing and Retrieving


Data
Android provides several ways to store and share data, including access
to the file-system, a local relational database through SQLite, and a
preferences system that allows you to store simple key/value pairs within
applications. In this chapter, we’ll start with preferences and you’ll create
a small sample application to exercise those concepts. From there, you’ll
create another sample application to examine using the filesystem to
store data, both internal to our application and external using the
platform’s Secure Digital (SD) card support. You’ll also see how to create
and access a database.

Beyond the basics, Android also allows applications to share data through
a clever URI-based approach called a ContentProvider. This technique
combines several other Android concepts, such as the URI-based style of
intents and the Cursor result set seen in SQLite, to make data accessible
across different applications. To demonstrate how this works, you’ll create
another small sample application that uses built-in providers, then we’ll
walk through the steps required to create your own ContentProvider.
Using preferences

If you want to share simple application data from one Activity to


another, use a SharedPreferences object. You can save and retrieve
data, and also choose whether to make preferences private to your
application or accessible to other applications on the same device.

Working with SharedPreferences


You access a SharedPreferences object through your current Context, such as the Activity
or Service. Context defines the method getSharedPreferences(String name, int
accessMode) that allows you to get a preferences handle. The name you specify will be the
name for the file that backs these preferences. If no such file exists when you try to get
preferences, one is automatically created. The access mode refers to what permissions you
want to allow.
Persisting data to a database
Android conveniently includes a built-in relational database. SQLite
doesn’t have all the features of larger client/server database products,
but it includes everything you need for local data storage. At the
same time, it’s quick and relatively easy to work with.

Building and accessing a database


To use SQLite, you have to know a bit about SQL in general. If you need
to brush up on the background of the basic commands, such as CREATE,
INSERT, UPDATE, DELETE, and SELECT, then you might want to take a
look at the SQLite documentation at http://www.sqlite.org/lang.html.
Working with ContentProvider classes
A ContentProvider in Android shares data between applications. Each
application usually runs in its own process. By default, applications can’t access
the data and files of other applications. We explained earlier that you can make
preferences and files available across application boundaries with the correct
permissions and if each application knows the context and path. This solution
applies only to related applications that already know details about one
another. In contrast, with a ContentProvider you can publish and expose a
particular data type for other applications to query, add, update, and delete,
and those applications don’t need to have any prior knowledge of paths,
resources, or who provides the content.

The canonical ContentProvider in Android is the contacts list, which provides


names, addresses, and phone numbers. You can access this data from any
application by using the correct URI and a series of methods provided by the
Activity and ContentResolver classes to retrieve and store data. You’ll learn
more about ContentResolver as we explore provider details. One other data-
related concept that a ContentProvider offers is the Cursor, the same object we
used previously to process SQLite database result sets.

You might also like