0% found this document useful (0 votes)
54 views

Android Content Providers: Trí PH M - 2012

This document provides an overview of content providers in Android. It discusses how content providers manage access to central data repositories and act as a standard interface for applications to share data. The document also outlines the key steps to developing a custom content provider, including extending the ContentProvider class, defining URIs and permissions, and implementing CRUD methods like query(), insert(), update(), and delete(). It provides an example of accessing an existing provider and the User Dictionary provider.

Uploaded by

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

Android Content Providers: Trí PH M - 2012

This document provides an overview of content providers in Android. It discusses how content providers manage access to central data repositories and act as a standard interface for applications to share data. The document also outlines the key steps to developing a custom content provider, including extending the ContentProvider class, defining URIs and permissions, and implementing CRUD methods like query(), insert(), update(), and delete(). It provides an example of accessing an existing provider and the User Dictionary provider.

Uploaded by

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

Android

Content Providers
Trí Phạm – 2012
[email protected]
Android Content Providers

• Content Provider Basics


• Accessing Content Provider
• Developing Custom Content
Provider
• Notepad Sample Project
CONTENT PROVIDERS
BASIC
Content Providers Basic

• A content provider manages access to a central repository of


data..
• The provider is part of an Android application, which often
provides its own UI for working with the data.
• However, content providers are primarily intended to be used
by other applications, which access the provider using a
provider client object.
• Together, providers and provider clients offer a consistent,
standard interface to data that also handles inter-process
communication and secure data access.
Content Providers Basic

Table 1: Sample user dictionary table.

word app id frequency locale _ID


mapreduce user1 100 en_US 1
precompiler user14 200 fr_FR 2
applet user2 225 fr_CA 3
const user1 255 pt_BR 4
int user5 100 en_UK 5
ACCESSING CONTENT
PROVIDER
Accessing Content Provider

• A content provider offers methods which correspond to the


basic CRUD (Create/Read/Update/Delete) functions of
persistent storage.
• An application accesses the data from a content provider
with a ContentResolver client object. This object has
methods that call identically-named methods in the provider
object.
• A content provider is identified by a content URI.
Accessing Content Provider

• Example of getting a list of words from the User Dictionary provider:

• The content URI of the words table is:


content://user_dictionary/words
• Read permission for accessing the content provider is also needed
in the manifest file:
DEVELOPING CUSTOM
CONTENT PROVIDER
Developing Custom Content
Provider
1. Extend the
ContentProvider
class.
2. In the onCreate()
method, create a
new instance of the
database helper
class.
Developing Custom Content
Provider

Suppose, we need to provide access to 2 tables through this


single content provider. As we have only one method per CRUD
operation, we need a way to differentiate between accesses to
these two tables.
3.We need to define content URI paths to each table. These are
defined in a public final class which can be used by both
provider and user as a contract: (see next slide)
Developing Custom Content
Provider
Developing Custom Content
Provider

Now comes the issue of differentiating between paths. The idea


is to match a URI and then taking appropriate actions for the
corresponding table path.
4.Add a UriMatcher to the provider and add expected URI
patterns to it.
5.In the query() method, get the appropriate table name from the
URI.
Developing Custom Content
Provider
Developing Custom Content
Provider

6. Now write the actual query method:

• You should add this URI to notification observables by calling


setNotificationUri() so that if this cursor is directly used in a
ListView, updating or inserting or deleting data in the table
represented by this URI would notify the ListView of this data change.
Developing Custom Content
Provider

7. insert, update and delete methods are similar.


– insert() returns the Uri with the newly
inserted ID appended.
– update() and delete() returns the
number of rows affected.
– You should call
notifyChangeToContentObservers(u
ri); before returning from these methods.
Developing Custom Content
Provider
We need to provide MIME type of the data returned by a URI.

8.The overridden method getType(Uri uri) needs to be filled-in.


– For common types of data such as as text, HTML, or JPEG, getType() should
return the standard MIME type for that data.
– For content URIs that point to a row or rows of table data, getType() should
return a MIME type in Android's vendor-specific MIME format:
• Type part: vnd
• Subtype part:
– If the URI pattern is for a single row: android.cursor.item/
– If the URI pattern is for more than one row: android.cursor.dir/
• Provider-specific part: vnd.<name>.<type>
– You supply the <name> and <type>.
– The <name> value should be globally unique, and the <type> value
should be unique to the corresponding URI pattern.
– A good choice for <name> is your company's name or some part of
your application's Android package name.
– A good choice for the <type> is a string that identifies the table
associated with the URI.
Developing Custom Content
Provider
• Content type defined in the contract class:
Developing Custom Content
Provider
• getType() method in the provider class:
Developing Custom Content
Provider
9. We need to declare the provider in the manifest.xml
file:
Developing Custom Content
Provider
10. Finally, we need to define permissions for applications who wish to access the
provider.
Different forms of permissions:
• Single read-write provider-level permission
– One permission that controls both read and write access to the entire provider,
specified with the android:permission attribute of the <provider> element
in manifest.xml.
• Separate read and write provider-level permission
– A read permission and a write permission for the entire provider.
– Specified with the android:readPermission and
android:writePermission attributes of the <provider> element.
– They take precedence over the permission required by android:permission.
• Path-level permission
– Read, write, or read/write permission for a content URI in your provider.
– You specify each URI you want to control with a <path-permission> child
element of the <provider> element.
• Temporary permission
– A permission level that grants temporary access to an application, even if the
application doesn't have the permissions that are normally required.
Developing Custom Content
Provider
• Permission defined in manifest.xml of the provider:

• Permission defined in manifest.xml of the user:


NOTEPAD SAMPLE
PROJECT
Q&A

You might also like