Android contacts for reading, querying, adding

Source: Internet
Author: User

Add two read and Write permissions first:

<uses-permission android:name= "Android.permission.READ_CONTACTS"/>

<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>

PackageCom.eboy.test;

ImportJava.util.ArrayList;

ImportAndroid.content.ContentProviderOperation;
ImportAndroid.content.ContentResolver;
ImportAndroid.database.Cursor;
ImportAndroid.net.Uri;
ImportAndroid.test.AndroidTestCase;
ImportAndroid.util.Log;

Public classContecttestextendsAndroidtestcase {

Private Static FinalString TAG = "Testcontact";

// Check all contact names, phone numbers, email
Public voidTestcontact ()throwsException {
Uri uri = uri.parse ("content://com.android.contacts/contacts");
Contentresolver resolver = GetContext (). Getcontentresolver ();
cursor cursor = resolver.query (URI,Newstring[]{"_id"},NULL,NULL,NULL);
while(Cursor.movetonext ()) {
intContractid = cursor.getint (0);
StringBuilder SB =NewStringBuilder ("contractid=");
Sb.append (Contractid);
URI = Uri.parse ("content://com.android.contacts/contacts/" + Contractid + "/data");
Cursor Cursor1 = Resolver.query (URI,Newstring[]{"MimeType", "Data1", "Data2"},NULL,NULL,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)) {// is the 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 of the designated phone, email
Public voidTestcontactnamebynumber ()throwsException {
String number = "18052369652";
Uri uri = uri.parse ("content://com.android.contacts/data/phones/filter/" + number);
Contentresolver resolver = GetContext (). Getcontentresolver ();
cursor cursor = resolver.query (URI,Newstring[]{"Display_name"},NULL,NULL,NULL);
if(Cursor.movetofirst ()) {
String name = cursor.getstring (0);
LOG.I (TAG, name);
}
Cursor.close ();
}

// Add a contact, use a transaction
Public voidTestaddcontact ()throwsException {
Uri uri = uri.parse ("content://com.android.contacts/raw_contacts");
Contentresolver resolver = GetContext (). Getcontentresolver ();
arraylist<contentprovideroperation> operations =NewArraylist<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", "Shang")
. 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", "[email protected]")
. Withvalue ("Data2", "2")
. build ();
Operations.add (OP4);

Resolver.applybatch ("com.android.contacts", operations);
}

Android contacts for reading, querying, adding

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.