1.权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
2.通过提供的内容供应商访问通信录(uri)
content://com.android.contacts/contacts 操作的数据是联系人信息Uri
content://com.android.contacts/data/phones 联系人电话Uri
content://com.android.contacts/data/emails 联系人Email Uri
android.provider.ContactsContract.RawContacts
android.provider.ContactsContract.Data
android.provider.ContactsContract.CommonDataKinds.StructuredName
android.provider.ContactsContract.CommonDataKinds.Phone
android.provider.ContactsContract.CommonDataKinds.Email
android.provider.ContactsContract.CommonDataKinds.StructuredPostal
android.provider.ContactsContract.CommonDataKinds.Website
android.provider.ContactsContract.CommonDataKinds.Organization
android.provider.ContactsContract.CommonDataKinds.Im
android.provider.ContactsContract.CommonDataKinds.Nickname
android.provider.ContactsContract.CommonDataKinds.Note
蓝色字体均为android提供的常量.
//android.provider.ContactsContract.RawContacts
//android.provider.ContactsContract.CommonDataKinds.StructuredName;
//android.provider.ContactsContract.Data
ContentValues values = new ContentValues();
values.put(RawContacts.DISPLAY_NAME_PRIMARY, "pccc xu");
values.put(RawContacts.DISPLAY_NAME_ALTERNATIVE, "xu,pccc");
Uri uri =getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//插入name电话号码values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, "显示名");
values.put(StructuredName.FAMILY_NAME, "姓");
getContentResolver().insert(Data.CONTENT_URI, values);
//插入电话信息
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1345678");
values.put(Phone.TYPE, Phone.TYPE_HOME);
getContentResolver().insert(Data.CONTENT_URI, values);
//3.插入data表数据(Im.QQ)
values.clear();
values.put(Data.RAW_CONTACT_ID, rawid);//外键值
values.put(Data.MIMETYPE, Im.CONTENT_ITEM_TYPE);
values.put(Im.PROTOCOL, Im.PROTOCOL_QQ);
values.put(Im.DATA, "3456789");
r.insert(Data.CONTENT_URI, values);
Android访问通讯录(查询)