Android Application Development: Content Provider
Android Application Development: Content Provider
Content Provider
Application Building Blocks Android Component
• UI Component Typically
Activity Corresponding to one screen.
Activities Services
1. Provides User Interface 1. No User Interface
2. Usually represents a Single 2. Runs in Background
Screen
3. Extends the Service Base Class
3. Can contain one/more Views
4. Extends the Activity Base class
• There are no common storage area that all Android application can
access.
• The only way: to share data across applications: Content Provider
• Content providers store and retrieve data and make it accessible to all
applications.
Content Provider Android Component
Content Provider Android Component
Android Content Provider Basics
Android ships with a number of content providers for common data types:
1. Audio
2. Video
3. Images
4. Personal contact information etc
Content Provider provides the way to share the data between multiple applications.
For example, contact data is used by multiple applications (Dialer, Messaging etc.)
and must be stored in Content Provider to have common access.
A content provider is a class that implements a standard set of methods to let other
applications store and retrieve the type of data that is handled by that content provider.
Android Content Provider Basics
Here are some of Android's most useful built-in content providers, along
with a description of the type of data they're intended to store
Content providers expose their data as a simple table on a database model, where
each row is a record and each column is data of a particular type and meaning.
For example, information about people and their phone numbers might be exposed
as follows:
Every record includes a numeric _ID field that uniquely identifies the record within
the table. IDs can be used to match records in related tables — for example, to
find a person's phone number in one table and pictures of that person in another.
A query returns a Cursor object that can move from record to record and column
to column to read the contents of each field. It has specialized methods for reading
each type of data. So, to read a field, you must know what type of data the field
contains.
Querying Data (Content Provider : URI) Android Content Provider
1. Each content provider exposes a public URI that uniquely identifies its data set.
2. A content provider that controls multiple data sets (multiple tables) exposes a
separate URI for each one.
3. All URIs for providers begin with the string "content://".
The content: scheme identifies the data as being controlled by a content provider.
URI samples:
<standard_prefix>://<authority>/<data_path>/<id>
For example, to retrieve all the bookmarks stored by our web browsers (in Android):
content://browser/bookmarks
To retrieve a particular contact, you can specify the URI with a specific ID:
content://contacts/people/3
Querying Data (Content Provider : URI) Android Content Provider
If we are querying a particular record, you also need the ID for that record.
• Although this is the general form of the query, query URIs are arbitrary and
confusing.
• For this android provide list of helper classes in android.provider package that define
these query Strings.
So we do not need to know the actual URI value for different data types.
So it will be easy to query data.
content://media/internal/images/ MediaStore.Images.Media.INTERNAL_CONTENT_URI
content://contacts/people/ Contacts.People.CONTENT_URI
content://contacts/people/45 Uri person = ContentUris.withAppendedId(People.CONTENT_URI, 23);
To query about specific record we have to use same CONTENT_URI and must append
specific ID.
Querying Data (Content Provider : URI) Android Content Provider
Parameters:
1. URI
2. The names of the data columns that should be returned. A null value
returns all columns.
3. A filter detailing which rows to return, formatted as an SQL WHERE clause
(excluding the WHERE itself). A null value returns all rows.
4. Selection arguments
5. A sorting order for the rows that are returned, formatted as an
SQL ORDER BY clause (excluding the ORDER BY itself). A null value returns
the records in the default order for the table, which may be unordered.
Cursor query(
Uri uri, // ContentProvider Uri
String[] projection // Columns to retrieve
String selection // SQL selection pattern
String[] selectionArgs // SQL pattern args
String sortOrder // Sort order
)
Querying Data (Code) Android Content Provider
String Name =
cur.getString(cur.getColumnIndex( ContactsContract.Contacts.DI
SPLAY_NAME ));
if(Integer.parseInt(cur.getString(
cur.getColumnIndex( ContactsContract.Contacts.HAS_PHONE_NUMBER
))) > 0) {
Cursor pCur =
cr.query(ContactsContract.CommonDataKinds.Phone.
CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ ID + "
= ?", new String[]{id},null);
while (pCur.moveToNext()) {
String Phone = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER)) ;
Log.i(MainActivity.this + " ", "Name: " + Name + "Number: "
+ Phone);
record[i] = Name + "\n" + Phone;
}
i++;
}}}
return record;
Querying Data (Content Provider: Query Example) Android Content Provider
Now, We run the app and in emulator we see a black screen as the emulator
has no contact
data:
Querying Data (Content Provider: Query Example) Android Content Provider
App 1 App 2
(Contacts) (our app)
}
Querying Data (Insert New Contact) Android Content Provider