When an external application needs to add, delete, modify, and query data in contentprovider, you can use the contentresolver class to complete the operation. To obtain the contentresolver object, you can use getcontentresolver () provided by activity () method. The contentresolver class provides four methods for the same signature as the contentprovider class:
Public URI insert (URI Uri, contentvalues values)
This method is used to add data to contentprovider.
Public int Delete (URI Uri, string selection, string [] selectionargs)
This method is used to delete data from contentprovider.
Public int Update (URI Uri, contentvalues values, string selection, string [] selectionargs)
This method is used to update data in contentprovider.
Public cursor query (URI Uri, string [] projection, string selection, string [] selectionargs, string sortorder)
This method is used to obtain data from contentprovider.
The first parameter of these methods is Uri, which indicates the contentprovider to be operated and the data to be operated on. Assume that the given parameter is Uri. parse ("Content: // CN. itcast. providers. personprovider/person/10), then the host name will be CN. itcast. providers. the contentprovider of personprovider performs operations. The operation data is the record with the ID of 10 in the person table.
Use contentresolver to add, delete, modify, and query data in contentprovider:
Contentresolver resolver = getcontentresolver ();
Uri uri = URI. parse ("content: // CN. itcast. provider. personprovider/person ");
// Add a record
Contentvalues values = new contentvalues ();
Values. Put ("name", "itcast ");
Values. Put ("Age", 25 );
Resolver. insert (Uri, values );
// Obtain all records in the person table
Cursor cursor = resolver. Query (Uri, null, "personid DESC ");
While (cursor. movetonext ()){
Log. I ("contenttest", "personid =" + cursor. getint (0) + ", name =" + cursor. getstring (1 ));
}
// Change the name field value of the record whose ID is 1 to liming.
Contentvalues updatevalues = new contentvalues ();
Updatevalues. Put ("name", "liming ");
Uri updateiduri = contenturis. withappendedid (Uri, 2 );
Resolver. Update (updateiduri, updatevalues, null, null );
// Delete the record with ID 2
Uri deleteiduri = contenturis. withappendedid (Uri, 2 );
Resolver. Delete (deleteiduri, null, null );
If the visitor of contentprovider needs to know that the data in contentprovider has changed, call getcontentresolver () when the data in contentprovider changes (). notifychange (Uri, null) to notify visitors registered on this URI, for example:
Public class personcontentprovider extends contentprovider {
Public URI insert (URI Uri, contentvalues values ){
DB. insert ("person", "personid", values );
Getcontext (). getcontentresolver (). policychange (Uri, null );
}
}
If the visitor of contentprovider needs to be notified of data changes, you must use contentobserver to listen to the data (the data is described in the URI). When the notification of data changes is received, the system will call the onchange () method of contentobserver:
Getcontentresolver (). registercontentobserver (URI. parse ("content: // CN. itcast. providers. personprovider/person "),
True, new personobserver (new handler ()));
Public class personobserver extends contentobserver {
Public personobserver (handler ){
Super (handler );
}
Public void onchange (Boolean selfchange ){
// Here you can perform corresponding business processing
}
}
Add the permission to read contact information
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
The data of the content: // com. Android. Contacts/contacts operation is the contact information Uri.
Content: // com. Android. Contacts/data/phones contact number URI
Content: // com. Android. Contacts/data/emails contact email URI
Read contact information
Cursor cursor = getcontentresolver (). Query (contactscontract. Contacts. content_uri, null );
While (cursor. movetonext ()){
String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));
String name = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));
Cursor phones = getcontentresolver (). Query (contactscontract. commondatakinds. Phone. content_uri, null, contactscontract. example. Phone. contact_id + "=" + contactid, null, null );
While (phones. movetonext ()){
String phonenumber = phones. getstring (phones. getcolumnindex (
Contactscontract. commondatakinds. Phone. Number ));
Log. I ("rongactivity", "phonenumber =" + phonenumber );
}
Phones. Close ();
Cursor emails = getcontentresolver (). Query (contactscontract. commondatakinds. Email. content_uri,
Null,
Contactscontract. commondatakinds. Email. contact_id + "=" + contactid,
Null, null );
While (emails. movetonext ()){
// This wocould allow you get several email addresses
String emailaddress = emails. getstring (emails. getcolumnindex (contactscontract. commondatakinds. Email. Data ));
Log. I ("rongactivity", "emailaddress =" + emailaddress );
}
Emails. Close ();
}
Cursor. Close ();