1. Add read/write permissions
XHTML code
- <Uses-Permission
Android: Name = "android. Permission. read_contacts"
/>
- <Uses-Permission
Android: Name = "android. Permission. write_contacts"
/>
<Uses-Permission Android: Name = "android. Permission. read_contacts"/> <uses-Permission Android: Name = "android. Permission. write_contacts"/>
Contact information URI:
Content: // com. Android. Contacts/contacts
Contact number URI:
Content: // com. Android. Contacts/data/phones
Contact email URI:
Content: // com. Android. Contacts/data/emails
(Recommended) You can also obtain the contact information URI: URI uri = contactscontract. Contacts. content_uri;
2. query and add Contacts (Unit Test Cases)
Java code
- Public
Class contacttest
Extends androidtestcase
- {
- Private
Static
Final string tag =
"Contacttest ";
- /**
- * Obtain the simple information of all contacts in the address book.
- * @ Throws throwable
- */
- Public
Void testgetallcontact ()
Throws throwable
- {
- // Obtain the URI of the contact information
- Uri uri = contactscontract. Contacts. content_uri;
- // Obtain contentresolver
- Contentresolver = This. getcontext (). getcontentresolver ();
- // Query data and return cursor
- Cursor cursor = contentresolver. Query (Uri, null,
Null,
Null,
Null );
- While (cursor. movetonext ())
- {
- Stringbuilder sb = new stringbuilder ();
- // Obtain the contact ID
- String contactid = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id ));
- // Obtain the contact name
- String name = cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. display_name ));
- // Construct the contact information
- SB. append ("contactid ="). append (contactid). append (", name ="). append (name );
- // Query telephone data operations
- Cursor phones = contentresolver. Query (contactscontract. commondatakinds. Phone. content_uri,
- Null,
- Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid,
- Null,
Null );
- While (phones. movetonext ())
- {
- String phonenumber = phones. getstring (phones. getcolumnindex (
- Contactscontract. commondatakinds. Phone. Number ));
- // Add phone information
- SB. append (", phone ="). append (phonenumber );
- }
- Phones. Close ();
- // Query email-type data operations
- Cursor emails = contentresolver. Query (contactscontract. commondatakinds. Email. content_uri,
- Null,
- Contactscontract. commondatakinds. Email. contact_id + "=" + contactid,
- Null,
Null );
- While (emails. movetonext ())
- {
- String emailaddress = emails. getstring (emails. getcolumnindex (
- Contactscontract. commondatakinds. Email. Data ));
- // Add email information
- SB. append (", email ="). append (emailaddress );
- }
- Emails. Close ();
- Log. I (TAG, SB. tostring ());
- }
- Cursor. Close ();
- }
- /** Method 1 for adding a contact:
- * First, execute a null value insert to rawcontacts. content_uri to obtain rawcontactid returned by the system.
- * In this case, the data table is inserted later. Only when null values are inserted can the inserted contacts be visible in the address book.
- */
- Public
Void testinsert ()
- {
- Contentvalues values = new contentvalues ();
- // First, execute a null value insert to rawcontacts. content_uri to obtain rawcontactid returned by the system.
- Uri rawcontacturi = This. getcontext (). getcontentresolver (). insert (rawcontacts. content_uri, values );
- // Obtain the ID
- Long rawcontactid = contenturis. parseid (rawcontacturi );
- // Input name data to the data table
- Values. Clear ();
- Values. Put (data. raw_contact_id, rawcontactid); // Add an ID
- Values. Put (data. mimetype, structuredname. content_item_type); // Add content type (mimetype)
- Values. Put (structuredname. given_name, "Kaifeng from South"); // Add the name to the first name position.
- This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
- // Input telephone data to the data table
- Values. Clear ();
- Values. Put (data. raw_contact_id, rawcontactid );
- Values. Put (data. mimetype, phone. content_item_type );
- Values. Put (phone. Number, "13921009789 ");
- Values. Put (phone. type, phone. type_mobile );
- This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
- // Input email data to the data table
- Values. Clear ();
- Values. Put (data. raw_contact_id, rawcontactid );
- Values. Put (data. mimetype, email. content_item_type );
- Values. Put (email. Data, "kesenhoo@gmail.com ");
- Values. Put (email. type, email. type_work );
- This. getcontext (). getcontentresolver (). insert (Android. provider. contactscontract. Data. content_uri, values );
- }
- /** Method 2 for adding a contact:
- * Add Contacts in batches
- * @ Throws throwable
- */
- Public
Void testsave ()
Throws throwable
- {
- // Official Document location: reference \ Android \ provider \ contactscontract.rawcontacts.html
- // Create an arraylist to store batch Parameters
- Arraylist <contentprovideroperation> Ops = new arraylist <contentprovideroperation> ();
- Int rawcontactinsertindex = ops. Size ();
- Ops. Add (contentprovideroperation. newinsert (rawcontacts. content_uri)
- . Withvalue (rawcontacts. account_type, null)
- . Withvalue (rawcontacts. account_name, null)
- . Build ());
- // Official Document location: reference \ Android \ provider \ contactscontract.data.html
- // Withvaluebackreference the ID of the preceding contact
- Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
- . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
- . Withvalue (data. mimetype, structuredname. content_item_type)
- . Withvalue (structuredname. given_name, "James ")
- . Build ());
- Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
- . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
- . Withvalue (data. mimetype, phone. content_item_type)
- . Withvalue (phone. Number, "13671323809 ")
- . Withvalue (phone. type, phone. type_mobile)
- . Withvalue (phone. label, "mobile phone number ")
- . Build ());
- Ops. Add (contentprovideroperation. newinsert (Android. provider. contactscontract. Data. content_uri)
- . Withvaluebackreference (data. raw_contact_id, rawcontactinsertindex)
- . Withvalue (data. mimetype, email. content_item_type)
- . Withvalue (email. Data, "kesen@gmail.com ")
- . Withvalue (email. type, email. type_work)
- . Build ());
- Contentproviderresult [] Results = This. getcontext (). getcontentresolver ()
- . Applybatch (contactscontract. Authority, OPS );
- For (contentproviderresult result: Results)
- {
- Log. I (TAG, result. Uri. tostring ());
- }
- }
- }