ContentProvider Add contacts and get contacts to Contacts

Source: Internet
Author: User

In Android, you can use Contentresolver to add, delete, modify, and query the data in a communication record.

Add the following two permissions when you work with a contact

<!--add permissions to an action contact--
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>
<!--contact-related URIs--
The data for the content://com.android.contacts/contacts operation is the contact information URI.
Content://com.android.contacts/data/phones Contact Phone URI
Content://com.android.contacts/data/emails Contact Person Email Uri

I. Manifest document

<?xml version= "1.0" encoding= "Utf-8"?>
<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"
Package= "Com.ljq.contact" android:versioncode= "1"
Android:versionname= "1.0" >
<application android:icon= "@drawable/icon"
Android:label= "@string/app_name" >
<uses-library android:name= "Android.test.runner"/>
<activity android:name= ". Contactactivity "
Android:label= "@string/app_name" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category
Android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

</application>
&LT;USES-SDK android:minsdkversion= "7"/>
<instrumentation
Android:name= "Android.test.InstrumentationTestRunner"
Android:targetpackage= "Com.ljq.contact"
Android:label= "Tests for My App"/>
<!--Add Contact permissions--
<uses-permission android:name= "Android.permission.READ_CONTACTS"/>
<uses-permission android:name= "Android.permission.WRITE_CONTACTS"/>

</manifest>

Get contacts and Add contacts

Package com.ljq.contact;

Import java.util.ArrayList;

Import android.content.ContentProviderOperation;
Import Android.content.ContentProviderResult;
Import Android.content.ContentResolver;
Import Android.content.ContentUris;
Import android.content.ContentValues;
Import Android.database.Cursor;
Import Android.net.Uri;
Import Android.provider.ContactsContract;
Import Android.provider.ContactsContract.Data;
Import android.provider.ContactsContract.RawContacts;
Import Android.provider.ContactsContract.CommonDataKinds.Email;
Import Android.provider.ContactsContract.CommonDataKinds.Phone;
Import Android.provider.ContactsContract.CommonDataKinds.StructuredName;
Import Android.test.AndroidTestCase;
Import Android.util.Log;

public class Contacttest extends androidtestcase{
private static final String TAG = "Contacttest";

/**
* Get contacts in Contacts
*/
public void Testgetcontact () {
Contentresolver contentresolver = This.getcontext (). Getcontentresolver ();
Uri uri = uri.parse ("content://com.android.contacts/contacts");
cursor cursor = contentresolver.query (URI, NULL, NULL, NULL, NULL);
while (Cursor.movetonext ()) {
Get Name of contact person
StringBuilder sb = new StringBuilder ();
String contactId = cursor.getstring (Cursor.getcolumnindex (contactscontract.contacts._id));
String name = cursor.getstring (Cursor.getcolumnindex (ContactsContract.Contacts.DISPLAY_NAME));
Sb.append ("contactid="). Append (ContactId). Append (", name="). Append (name);

Get Contact phone number
Cursor phones = Contentresolver.query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
Null
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactId,
NULL, NULL);
while (Phones.movetonext ()) {
String phone = phones.getstring (Phones.getcolumnindex ("data1"));
Sb.append (", phone="). Append (phone);
}

Get Contact Email
Cursor emails = contentresolver.query (ContactsContract.CommonDataKinds.Email.CONTENT_URI,
Null
ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + ContactId,
NULL, NULL);
while (Emails.movetonext ()) {
String email = emails.getstring (Emails.getcolumnindex ("data1"));
Sb.append (", email="). Append (email);
}
LOG.I (TAG, sb.tostring ());
}
}

/**
* First a null insertion is performed to Rawcontacts.content_uri to get the Rawcontactid returned by the system
*
* This is the data table that is inserted later, only the null value is inserted to make the inserted contact visible in the Address book.
*/
public void Testinsert () {
Contentvalues values = new Contentvalues ();
First, a null insertion is performed to Rawcontacts.content_uri to get the Rawcontactid returned by the system
Uri Rawcontacturi = This.getcontext (). Getcontentresolver (). Insert (Rawcontacts.content_uri, values);
Long Rawcontactid = Contenturis.parseid (Rawcontacturi);

Enter the name data to the data table
Values.clear ();
Values.put (data.raw_contact_id, Rawcontactid);
Values.put (Data.mimetype, Structuredname.content_item_type);
Values.put (Structuredname.given_name, "Zhangsan");
This.getcontext (). Getcontentresolver (). Insert (
Android.provider.ContactsContract.Data.CONTENT_URI, values);

Incoming phone data
Values.clear ();
Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);
Values.put (Data.mimetype, Phone.content_item_type);
Values.put (Phone.number, "5554");
Values.put (Phone.type, phone.type_mobile);
This.getcontext (). Getcontentresolver (). Insert (
Android.provider.ContactsContract.Data.CONTENT_URI, values);

Enter the data into your email
Values.clear ();
Values.put (Android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, Rawcontactid);
Values.put (Data.mimetype, Email.content_item_type);
Values.put (Email.data, "[Email protected]");
Values.put (Email.type, email.type_work);
This.getcontext (). Getcontentresolver (). Insert (
Android.provider.ContactsContract.Data.CONTENT_URI, values);
}

/**
* Bulk Add contacts in the same transaction
*/
public void Testsave () throws throwable{
Document Location: reference\android\provider\contactscontract.rawcontacts.html
arraylist<contentprovideroperation> Ops = new arraylist<contentprovideroperation> ();
int rawcontactinsertindex = 0;
Ops.add (Contentprovideroperation.newinsert (Rawcontacts.content_uri)
. Withvalue (Rawcontacts.account_type, NULL)
. Withvalue (Rawcontacts.account_name, NULL)
. build ());

Document Location: reference\android\provider\contactscontract.data.html
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, "Lisi")
. 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, "5556")
. Withvalue (Phone.type, Phone.type_mobile)
. Withvalue (Phone.label, "")
. 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, "[Email protected]")
. 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 ());
}
}

}

ContentProvider Add contacts and get contacts to Contacts

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.