Understanding Content Providers :
Content provider is a term that is unique to Android
development that means nothing more than a datastore of data values, most often
found in the form of a SQLite database that is already part of the Android
operating system (OS). You can also create your own content providers for your application.
An Android content provider provides you with access to
sharable data structures commonly called databases. The basic procedure is as
follows:
- Get permission to read the database, and possibly to write to the database.
- Query (find) the data.
- Access (read) the data.
- Modify (add, change (overwrite), append to, or delete) the data.
In accessing data, you might simply read the data, write to
the data (i.e., change the values of the existing data); append (add) new data
onto the database structure; or delete existing data, based on the type and
level of security permissions that have been established in the AndroidManifest.xml
file.
Data can be in Android internal (system) memory, in a SQLite database, or
in external memory such as an SD card, or even on an external server that is
remote to the Android device itself.
Databases and Database Management Systems :
The usual way for content providers to provide data
structures for Android applications is via a database management system (DBMS).
A DBMS manages a database by providing ways for users to create databases, as
well as to populate them with data via reading and writing operations.
There is a complete open source DBMS right inside the
Android OS called SQLite. SQL is a relational DBMS (RDBMS). An RDBMS is based
on relationships that can be drawn between data arranged in tables.
The SQL in SQLite stands for structured query language. The
“Lite” or “Light” part delineates that this is a “lightweight” version of the
DBMS, intended for embedded use in consumer electronics devices, and not a full
blown version of SQL, as would be used on an advanced computer system such as a
database server.
In a DBMS, the highest level of data storage is the database
itself, which contains tables of data in rows and columns. Each table is
two-dimensional, where a row is called a record. Within each record are fields,
organized into columns, which contain the individual data items that make up
the records. Fields can contain different data types, such as numbers, text, or
even references to data that is stored somewhere else.
Note that there can be more than one table in a database
(and there usually is, for both performance and for organizational reasons). As
long as there is a key (a unique index) for each record in each table,
information for a single data entry can span more than one table using that
key, called an ID and designated via the constant “_ID” in Android SQLite databases.
For instance, if your key or _ID value is 217, your personal information and
phone information can be in two different tables stored under that same key
value.
The content provider classes that are provided with the
Android OS all use SQLite because it is compact and open source, and a part of
the Android OS. There is also an Android library of SQLite classes that can
access SQLite directly, for more advanced use.
Android Built-In Content Providers :
A significant number of SQLite-based database structures are
“hard-coded” into the Android OS to handle things that users expect from their
phones, iTVs, e-readers, and tablets, such as contact directories, address
books, calendars, camera picture storage, digital video storage, music libraries,
playlists, and so forth. The most extensive of these SQLite database structures
is the Contacts database, which contains many different tables (subdatabases)
for contact names, phone numbers, e-mails, preferences, social media settings,
and so forth.
Defining a Content Provider:
Before a content provider can be used, it must be registered
for use by your Android application. This is done by using some XML markup in
the AndroidManifest.xml file. The <provider> tag so aptly named, allows
us to define which content providers we are going to access once our application
is launched. Here’s an example <provider> tag for the Images content
provider:
<provider android:name =
"MediaStore.Images.ImageColumns" />
All Android content providers expose to developers a
publicly accessible unique reference, or address, if you will, to each
database. This address is called a URI, and the Android constant that points to
the data location within the database table is always called CONTENT URI.
Providers built-in to the OS do not need a <provider> declaration.
A content provider that provides access to multiple tables
will expose a unique URI for each table.Here are a couple examples of
predetermined Android URI constants:
- android.provider.ContactsContract.PhoneLookupColumns.CONTENT URI
- android.provider.ContactsContract.StreamItemPhotosColumns.CONTENT URI
No comments:
Post a Comment