Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

FACULTY OF INFORMATION TECHNOLOGY

SUBJECT: MOBILE DEVICE PROGRAMMING

Unit 6

CONTENT PROVIDER
Lecturer: Võ Tấn Dũng
votandung@yahoo.com
http://sites.google.com/site/votandungsg/
Content Provider
A content provider component supplies data from one application to others
on request. Such requests are handled by the methods of the
ContentResolver class. A content provider can use different ways to store its
data and the data can be stored in a database, in files, or even over a
network.

VÕ TẤN DŨNG
An introduction to
content providers
• URIs for content providers
• MIME types for content providers
• How to add supporting methods to the database class

VÕ TẤN DŨNG
Built-in Android content providers
• As mentioned earlier, a content provider allows multiple
apps to share the same data. This data can be stored in a
database or in files.
• Android includes content providers for your contacts,
calendar, settings, bookmarks, and media including images,
music, and video. That way, built-in and third-party apps can
share this data

VÕ TẤN DŨNG
URIs for content providers
• A URI (Uniform Resource Identifier) for a content provider includes the
name of the provider (the authority) and a path to the content.
• This path typically begins with a name that corresponds with a table or
file, and it can include an optional ID that points to an individual row in a
table.
• Within a path, each front slash begins a new segment

VÕ TẤN DŨNG
MIME types for content providers
• When you create a URI for a content provider, you need to
specify the type of content that’s returned by that URI. To do
that, you use a MIME type, which is an Internet standard for
defining types of content.

VÕ TẤN DŨNG
Add supporting methods to the database class
Description
• To make it easy to create a content provider, you can include some
supporting methods in your database class that provide lor the query,
insert, update, and delete operations.
• These methods should not close the database connection after
performing the operation.

In previous unit, we learned how to create a database class named TaskListDB. Now, we
add to this database class some supporting methods provide basic query, insert, update,
and delete operations for the Task table. These methods will be used by the content
provider (TaskListProvider).

VÕ TẤN DŨNG
Add supporting methods to the database class
• A

VÕ TẤN DŨNG
How to create a content provider
Description
• The class for a content provider must inherit the ContentProvider class.
• To initialize variables, your content provider class can override the
onCreate method of the ContentProvider class.

VÕ TẤN DŨNG
How to start a content provider class
• To begin, you declare a class like the TaskListProvider class that
extends the ContentProvider class.
• Within the TaskListProvider class:
• The first four statements declare the constants for the class.
• The first statement declares the constant for the authority that’s used within
the URL.
• Then, the next three statements declare constants that are used to determine
whether the URI matches one of the URIs that are supported by this class.

VÕ TẤN DŨNG
How to start a content provider class
• After declaring the constants, this class declares two instance variables. The
first instance variable is a TaskListDB object for working with the Task List
database. The second instance variable is a UriMatcher object for checking
whether a URI matches one of the supported URIs.

• The onCreate method contains the code that initializes these variables. This
method overrides the onCreate method of the ContentProvider class. As a
result, Android calls this method when it creates the content provider.

VÕ TẤN DŨNG
How to start a content provider class
• Within the onCreate method, the first statement creates the TaskListDB
object. Then, the next three statements create the UriMatcher object.
Of these statements, the first creates the UriMatcher object and uses
the NO_MATCH variable to specify the value that’s returned if no
matching URI is found. The second statement adds this URI:
content s //com.murach.tasklist.provider/tasks
to the UriMatcher object and uses the ALL_TASKS_URI constant to
specify the value that’s returned if a URI matches this URI. The third
statement adds this URI:
content://com.murach.tasklist.provider/tasks/#
and uses the SINGLE_TASK_URI constant to specify the value that’s
returned if a URI matches this URI. Here, the last segment uses the #
wildcard to specify any number. As a result, this URI pattern matches a
URI that specifies a single row like this:
content://com.murach.tasklist.provider/tasks/5
VÕ TẤN DŨNG
How to start a content provider class
• A constructor and method of the UriMatcher class

VÕ TẤN DŨNG
The TaskListProvider class
• A

VÕ TẤN DŨNG
How to provide for querying
Description
• To allow other apps to query your data, your content provider
class can override the query and getType methods of the
ContentProvider class.
• Every data access method of the ContentProvider class has a Uri
object as its first argument. This allows the method to determine
what table, row, or file to access.
• In a URI, you can use the # wildcard to match any number.

VÕ TẤN DŨNG
How to provide for querying
• A

VÕ TẤN DŨNG
How to provide for inserting rows
Description
• To allow other apps to insert rows into your database, you can override
the insert method of the ContentProvider class.
• The insert method should return a URI for the inserted row.

VÕ TẤN DŨNG
How to provide for inserting rows

VÕ TẤN DŨNG
How to provide for inserting rows
• The third statement (in the previous slide) uses several
methods of the Uri.Builder class to build the Uri object
for the row that was inserted. To do that, it appends the
ID for the row that was inserted to the URI argument.
• For example, if you insert a row and the database assigns
that row a primary key value of 14, this statement returns
this URI:
content://com.murach.tasklist.provider/tasks/14

VÕ TẤN DŨNG
How to provide for updating rows
Description
To allow other apps to update or delete rows in your
database, you can override the:
•update and
•delete methods
of the ContentProvider class.

VÕ TẤN DŨNG
How to provide for updating rows

VÕ TẤN DŨNG
How to provide for updating rows
• Update method: This method accepts four arguments (the URI,
a ContentValues object, a WHERE clause, and an array that contains the
arguments for the WHERE clause). This method returns an int value for
the count of rows that were updated.

• The update method supports two URls:


• (1) the URI that corresponds with the ALL_TASKS_URI constant and
• (2) the URI that corresponds with the SINGLE_TASK_UR1 constant.
• Update methode also returns the count of rows that were updated. If
the update is successful, this should return a count of 1

VÕ TẤN DŨNG
How to provide for deleting rows
• For deleting rows, the delete method works much like the update
method.

VÕ TẤN DŨNG
How to register a content provider
Description
• Before other apps can use your content provider class, you must
register it by adding a provider element to the AndroidManifest.xml file.

VÕ TẤN DŨNG
How to use a custom content provider
Description
• You can define constants for the columns and URI for the content
provider.
• You can call any methods of the content provider from the
ContentResolver object.

VÕ TẤN DŨNG
How to use a custom content provider

VÕ TẤN DŨNG
How to use a custom content provider

VÕ TẤN DŨNG
An example of custom content provider
• See at this page:
https://www.tutlane.com/tutorial/android/android-content-providers-
with-examples

VÕ TẤN DŨNG
Examples of buit-in content provider
• See at this page:
https://duythanhcse.wordpress.com/2013/06/14/bai-tap-33-su-dung-
contentprovider-trong-android/

VÕ TẤN DŨNG
END of Unit 6
(see at http://sites.google.com/site/votandungsg/)

VÕ TẤN DŨNG

You might also like