Contacts in the Android system also provide external data through ContentProvider. Here we can obtain all contacts, obtain contacts through phone numbers, add contacts, and use transactions to add Contacts.
Get all contacts
1. Contacts in the Android system provide data externally through ContentProvider.
2. database path:/data/com. android. providers. contacts/database/contacts2.db
3. We need to pay attention to three tables.
Raw_contacts: the contact id is saved.
Data: it is a many-to-one relationship with raw_contacts. It stores various contact data.
Mimetypes: Data Type
4. The authorites of the Provider is com. android. contacts.
5. The path for querying the raw_contacts table is contacts.
6. The path for querying the data table is contacts/#/data
This path is for connection query. to query the "mimetype" field, you can query the data in the mimetypes table according to "mimetype_id ".
7. First query raw_contacts to get the id of each contact. Then, use the id to query the corresponding data from the data table and classify the data according to mimetype.
Example:
// Query all contacts
Public void testGetAll (){
ContentResolver resolver = getContext (). getContentResolver ();
Uri uri = Uri. parse ("content: // com. android. contacts/contacts ");
Cursor idCursor = resolver. query (uri, new String [] {"_ id"}, null );
While (idCursor. moveToNext ()){
// Obtain the id in the raw_contacts table
Int id = idCursor. getInt (0 );
// Query the data in the data table based on the obtained ID
Uri = Uri. parse ("content: // com. android. contacts/" + id + "/data ");
Cursor dataCursor = resolver. query (uri, new String [] {"data1", "mimetype"}, null );
StringBuilder sb = new StringBuilder ();
Sb. append ("id =" + id );
// Query
While (dataCursor. moveToNext ()){
String data = dataCursor. getString (0 );
String type = dataCursor. getString (1 );
If ("vnd. android. cursor. item/name". equals (type ))
Sb. append (", name =" + data );
Else if ("vnd. android. cursor. item/phone_v2". equals (type ))
Sb. append (", phone =" + data );
Else if ("vnd. android. cursor. item/email_v2". equals (type ))
Sb. append (", email =" + data );
}
System. out. println (sb );
}
}
Obtain a contact by phone number
1. The system provides the ability to obtain data in the data table based on the phone number. The path is data/phones/filter /*
2. Replace "*" with the phone number to find the required data. Get "display_name" to get the display name of the contact.
Example:
// Query the contact name by phone number
Public void testGetName (){
ContentResolver resolver = getContext (). getContentResolver ();
Uri uri = Uri. parse ("content: // com. android. contacts/data/phones/filter/1111 ");
Cursor c = resolver. query (uri, new String [] {"display_name"}, null );
While (c. moveToNext ()){
System. out. println (c. getString (0 ));
}
}
Add contact
1. Insert the id to the raw_contacts table first. The path is raw_contacts.
2. Get the id and insert the data to the data table. The path is data.
Example:
// Add a contact
Ublic void testInsert (){
ContentResolver resolver = getContext (). getContentResolver ();
Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts ");
ContentValues values = new ContentValues ();
// Insert a record other than the ID to raw_contacts, all of which are NULL. The ID is automatically generated.
Long id = ContentUris. parseId (resolver. insert (uri, values ));
// Add a contact name
Uri = Uri. parse ("content: // com. android. contacts/data ");
Values. put ("raw_contact_id", id );
Values. put ("data2", "FHM ");
Values. put ("mimetype", "vnd. android. cursor. item/name ");
Resolver. insert (uri, values );
// Add a contact number
Values. clear (); // clear the previous data
Values. put ("raw_contact_id", id );
Values. put ("data1", "18600000000 ");
Values. put ("data2", "2 ");
Values. put ("mimetype", "vnd. android. cursor. item/phone_v2 ");
Resolver. insert (uri, values );
// Add a contact email
Values. clear ();
Values. put ("raw_contact_id", id );
Values. put ("data1", "zxx@itcast.cn ");
Values. put ("data2", "1 ");
Values. put ("mimetype", "vnd. android. cursor. item/email_v2 ");
Resolver. insert (uri, values );
Use transactions to add Contacts
1. the Provider is accessed multiple times when a contact is added. If an exception occurs during the process, data is incomplete. These operations should be placed in a transaction.
2. Use the ContentResolver's applyBatch (String authority, ArrayList <ContentProviderOperation> operations) method to execute multiple operations in one transaction.
Example:
// Use transactions to add Contacts
Public void testInsertBatch () throws Exception {
ContentResolver resolver = getContext (). getContentResolver ();
ArrayList <ContentProviderOperation> operations = new ArrayList <ContentProviderOperation> ();
ContentProviderOperation operation1 = ContentProviderOperation //
. NewInsert (Uri. parse ("content: // com. android. contacts/raw_contacts "))//
. WithValue ("_ id", null )//
. Build ();
Operations. add (operation1 );
ContentProviderOperation operation2 = ContentProviderOperation //
. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//
. WithValueBackReference ("raw_contact_id", 0 )//
. WithValue ("data2", "ZZH ")//
. WithValue ("mimetype", "vnd. android. cursor. item/name ")//
. Build ();
Operations. add (operation2 );
ContentProviderOperation operation3 = ContentProviderOperation //
. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//
. WithValueBackReference ("raw_contact_id", 0 )//
. WithValue ("data1", "18612312312 ")//
. WithValue ("data2", "2 ")//
. WithValue ("mimetype", "vnd. android. cursor. item/phone_v2 ")//
. Build ();
Operations. add (operation3 );
ContentProviderOperation operation4 = ContentProviderOperation //
. NewInsert (Uri. parse ("content: // com. android. contacts/data "))//
. WithValueBackReference ("raw_contact_id", 0 )//
. WithValue ("data1", "www.2cto.com ")//
. WithValue ("data2", "2 ")//
. WithValue ("mimetype", "vnd. android. cursor. item/email_v2 ")//
. Build ();
Operations. add (operation4 );
// Batch Execute multiple operations in the transaction
Resolver. applyBatch ("com. android. contacts", operations );
}
Author: Mu chenshou