Add two read and write permissions first:
<Uses-Permission Android: Name = "android. Permission. read_contacts"/>
<Uses-Permission Android: Name = "android. Permission. write_contacts"/>
Package com. eboy. test;
Import java. util. arraylist;
Import Android. content. contentprovideroperation;
Import Android. content. contentresolver;
Import Android. database. cursor;
Import android.net. Uri;
Import Android. Test. androidtestcase;
Import Android. util. log;
Public class contecttest extends androidtestcase {
Private Static final string tag = "testcontact ";
// Query the name, phone number, and email address of all contacts.
Public void testcontact () throws exception {
Uri uri = URI. parse ("content: // com. Android. Contacts/contacts ");
Contentresolver resolver = getcontext (). getcontentresolver ();
Cursor cursor = resolver. Query (Uri, new string [] {"_ id"}, null );
While (cursor. movetonext ()){
Int contractid = cursor. getint (0 );
Stringbuilder sb = new stringbuilder ("contractid = ");
SB. append (contractid );
Uri = URI. parse ("content: // com. Android. Contacts/contacts/" + contractid + "/Data ");
Cursor cursor1 = resolver. Query (Uri, new string [] {"mimetype", "data1", "data2"}, null );
While (cursor1.movetonext ()){
String data1 = cursor1.getstring (cursor1.getcolumnindex ("data1 "));
String mimetype = cursor1.getstring (cursor1.getcolumnindex ("mimetype "));
If ("Vnd. Android. cursor. Item/Name". Equals (mimetype) {// Yes Name
SB. append (", name =" + data1 );
} Else if ("Vnd. Android. cursor. Item/email_v2". Equals (mimetype) {// email
SB. append (", email =" + data1 );
} Else if ("Vnd. Android. cursor. Item/phone_v2". Equals (mimetype) {// mobile phone
SB. append (", phone =" + data1 );
}
}
Cursor1.close ();
Log. I (TAG, SB. tostring ());
}
Cursor. Close ();
}
// Query the contact name and email address of the specified phone number
Public void testcontactnamebynumber () throws exception {
String number = "18052369652 ";
Uri uri = URI. parse ("content: // com. Android. Contacts/data/phones/filter/" + number );
Contentresolver resolver = getcontext (). getcontentresolver ();
Cursor cursor = resolver. Query (Uri, new string [] {"display_name"}, null );
If (cursor. movetofirst ()){
String name = cursor. getstring (0 );
Log. I (TAG, name );
}
Cursor. Close ();
}
// Add contacts and use transactions
Public void testaddcontact () throws exception {
Uri uri = URI. parse ("content: // com. Android. Contacts/raw_contacts ");
Contentresolver resolver = getcontext (). getcontentresolver ();
Arraylist <contentprovideroperation> operations = new arraylist <contentprovideroperation> ();
Contentprovideroperation OP1 = contentprovideroperation. newinsert (URI)
. Withvalue ("account_name", null)
. Build ();
Operations. Add (OP1 );
Uri = URI. parse ("content: // com. Android. Contacts/Data ");
Contentprovideroperation OP2 = contentprovideroperation. newinsert (URI)
. Withvaluebackreference ("raw_contact_id", 0)
. Withvalue ("mimetype", "Vnd. Android. cursor. Item/Name ")
. Withvalue ("data2", "Yong Xiaoyong ")
. Build ();
Operations. Add (OP2 );
Contentprovideroperation OP3 = contentprovideroperation. newinsert (URI)
. Withvaluebackreference ("raw_contact_id", 0)
. Withvalue ("mimetype", "Vnd. Android. cursor. Item/phone_v2 ")
. Withvalue ("data1", "13539777967 ")
. Withvalue ("data2", "2 ")
. Build ();
Operations. Add (OP3 );
Contentprovideroperation op4 = contentprovideroperation. newinsert (URI)
. Withvaluebackreference ("raw_contact_id", 0)
. Withvalue ("mimetype", "Vnd. Android. cursor. Item/email_v2 ")
. Withvalue ("data1", "asdfasfad@163.com ")
. Withvalue ("data2", "2 ")
. Build ();
Operations. Add (op4 );
Resolver. applybatch ("com. Android. Contacts", operations );
}
}
/Files/jxgxy/readwritecontact.rar