Usage of contentProvider in Android

Source: Internet
Author: User

Share data with content providers

If you directly access a disk File (SDCard | File | SQLite database | preference), you need a lot of underlying interaction details. However, in this way, you can only access the application using URLs, unified Data Access methods.

Public class PersonContentProvider extends ContentProvider {

// The Content object can be obtained correctly in this lifecycle.

Public boolean onCreate (){

//...

}

Public Uri insert (Uri uri, ContentValues values );

....

}

<Application android: icon = "@ drawable/icon"

Android: label = "@ string/app_name">

<! -- Register a supplier -->

<Provider android: name = ". PersonContentProvider"

Android: authorities = "cn. itcast. providers. personprovider"/>

</Application>

URI:

Content: // cn. itcast. provider. personprovider/person/10

Scheme host name or authority path

Scheme: android requires content ://

Authority: uniquely identifies the supplier.

Path: indicates the data to be manipulated.

Converts a string to a Uri object.

Uri uri = Uri. parse ("content: // xxxx/person/10 ");

UriMatcher:

This class can be used to determine the Uri mode.

// If no match exists, UriMather. NO_MATCH is returned.

UriMatcher sMatcher = new UriMatcher (UriMatcher. NO_MATCH );

// Add a matched pattern string at a time and the value returned after the match

// $ {Authority }$ {path}

SMatcher. addURI ("cn. itcast. contentprovider", "person", 1 );

SMatcher. addURI ("cn. itcast. contentprovider", "person/#", 2 );

Switch (sMatcher. match (Uri )){

Case 1:

Case 2:

.....

}

// If you get the ID part behind the uri, you can use

Uri uri = Uri. parse ("content: // cn. itcast. provider. personprovider/person ");

Uri resultUri = ContentUris. withAppendedId (uri, 10); //... person/10

Uri. parse ("content: // cn. itcast. provider. personprovider/person/10 ")

Long personid = ContentUris. parseId (uri );

XxxContentProvider extends ContentProvider {

....

// This method is used to return the MIME type of the data represented by the current Url. If the operated data belongs to the set type,

// The MIME-type string should start with vnd. android. cursor. dir/

// For example, ''vnd. android. cursor. dir/person"

// If the data to be operated belongs to a non-set type, the MIME type string should

// Start with vnd. android. cursor. item/

// For example, vnd. android. cursor. item/person

Public String getType (Uri uri ){..};

}

Use the content parser (ContentResolver ):

ContentResolver resolver = getContentResolver ();

Uri uri = Uri. parse ("content: // cn. itcast. provider. personprovider/person ");

ContentValues values = new ContentValues ();

Values. put ("name", "itcast ");

Values. put ("age", 25 );

// Insert

Resolver. insert (uri, values );

// Query

Resolver. query (uri, null, "personid desc ");

// Update

Resolver. update (updateIdUri, updateValues, null, null );

// Delete

Resolver. delete (deleteIdUri, null, null );

Monitors data changes of contentprovider

// After the crud method in the supplier is completed, a notification of data changes should be issued to notify those interested in the change.

XxxContentProvider extends ContentProvider {

Public long insert (){

...

GetContext (). getContentResolver (). policychange (uri, null );

}

}

If the content parser needs to change data, it needs to use ContentObserver to listen to the uri. After receiving the notification, the callback processing method onChange ();

Context. getContentResolver (). registerContentObserver (uri, true, new MyContentObserver (new Handler ()));

Class MyContentObServer extends ContentObserer {

Public MyContentObServer (Handler handler ){

Super (handler );

}

Public void onChange (){...}

}

Listen to text messages sent (received) by users

When the system's built-in SMS program sends (or receives) text messages, it saves the text messages to the database through ContentProvider, and

Send a change notification. Use ContentObserver to listen to data changes and send

Messages are intercepted by ContentObserver.

// System

Context. getContentResolver (). policychange ("content: // sms", null );

// Client:

Context. getContentResolver (). registerObserver (uri, false, new

ContentObserver (new Handler ()){});

// Content: // sms/outbox

// Content: // sms/outbox

// Content: // sms/outbox

Cursor cursor = www.2cto.com

GetContentResolver (). query (Uri. parse ("content: // sms/outbox"), null,

Cursor. getInt (cursor. getColumnIndex ("_ id ")));

Cursor. getString (cursor. getColumnIndex ("address ")));

Cursor. getString (cursor. getColumnIndex ("body ")));

Cursor. getLong (cursor. getColumnIndex ("date ")));

Note:

<Uses-permission android: name = "android. permission. READ_SMS"/>

Author: to1297488504

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.