先说下bean
public class ContactBean {// 存放联系人的bean 信息
private String displayName;// 联系人的名字
private List<String> emails ;// 联系人email 的集合
private List<String> phones;// 电话的集合!
}
操作类!ContactUtils
以下有 获取的方法,和添加的方法我都测试过了,没任何问题!
你用的时候, 整个考过去, 改改就行了!
package com.example.testcontentprovider.utils;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.database.Cursor;
import android.net.Uri;
import android.os.RemoteException;
import android.provider.ContactsContract;
import android.util.Log;
import com.example.testcontentprovider.bean.ContactBean;
public class ContactUtils {
//[content://com.android.contacts/contacts]
private static final Uri CONTACTS_URI = ContactsContract.Contacts.CONTENT_URI;
//[content://com.android.contacts/data/phones]
private static final Uri PHONES_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//[content://com.android.contacts/data/emails]
private static final Uri EMAIL_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
private static final String _ID = ContactsContract.Contacts._ID;
private static final String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME;
private static final String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER;
private static final String CONTACT_ID = ContactsContract.Data.CONTACT_ID;
private static final String PHONE_NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER;
private static final String PHONE_TYPE = ContactsContract.CommonDataKinds.Phone.TYPE;
private static final String EMAIL_DATA = ContactsContract.CommonDataKinds.Email.DATA;
private static final String EMAIL_TYPE = ContactsContract.CommonDataKinds.Email.TYPE;
//[content://com.android.contacts/raw_contacts]
private static final Uri RAW_CONTACTS_URI = ContactsContract.RawContacts.CONTENT_URI;
//[content://com.android.contacts/data]
private static final Uri DATA_URI = ContactsContract.Data.CONTENT_URI;
private static final String ACCOUNT_TYPE = ContactsContract.RawContacts.ACCOUNT_TYPE;
private static final String ACCOUNT_NAME = ContactsContract.RawContacts.ACCOUNT_NAME;
private static final String RAW_CONTACT_ID = ContactsContract.Data.RAW_CONTACT_ID;
private static final String MIMETYPE = ContactsContract.Data.MIMETYPE;
private static final String NAME_ITEM_TYPE = ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE;
private static final String PHONE_ITEM_TYPE = ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE;
private static final int PHONE_TYPE_HOME = ContactsContract.CommonDataKinds.Phone.TYPE_HOME;
private static final int PHONE_TYPE_MOBILE = ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
private static final String EMAIL_ITEM_TYPE = ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE;
private static final int EMAIL_TYPE_HOME = ContactsContract.CommonDataKinds.Email.TYPE_HOME;
private static final int EMAIL_TYPE_WORK = ContactsContract.CommonDataKinds.Email.TYPE_WORK;
private static final String AUTHORITY = ContactsContract.AUTHORITY;
public static List getContacts(Context context){
// 存放所有联系人信息的集合!
List contactBeans = new ArrayList();
ContentResolver resolver =context.getContentResolver();
// 查找联系人表!
Cursor c = resolver.query(CONTACTS_URI, null, null, null, null);
// 开始遍历游标!
while (c.moveToNext()) {
// 存放单个联系人的集合!
ContactBean contactBean = new ContactBean();
// 得到联系人的_ID
int _id = c.getInt(c.getColumnIndex(_ID));
// 得到联系人的名字!
String displayName = c.getString(c.getColumnIndex(DISPLAY_NAME));
// 设置到contactBean 中!
contactBean.setDisplayName(displayName);
//Log.i(TAG, displayName);
ArrayList phones = new ArrayList();
ArrayList emails = new ArrayList();
// 根据联系人id 去查找对应的数据! 一下是查询条件!
String selection = CONTACT_ID + "=" + _id; //the 'where' clause
//获取手机号
// 获取电话号码的数量! 如果有肯定就>0. 如果没有就是0 就不需要去获取了!
int hasPhoneNumber = c.getInt(c.getColumnIndex(HAS_PHONE_NUMBER));
if (hasPhoneNumber > 0) {
// 有电话号码,就去电话号码那表中去获取数据就行了!
// PHONES_URI 代表电话号码那张表!一对多的关系, 一个联系人有多个电话号码, 多个电话能属于一个人, 双向
// 关联都成立,才是硬道理!
// 所以以下Phc 中 可以是个集合, 而且我敢肯定每条记录的区别,用phoneType 字段来区分开来!
Cursor phc = resolver.query(PHONES_URI, null, selection, null, null);
while (phc.moveToNext()) {
// 每遍历一次就,将类型和电话号码放到phones集合中!
String phoneNumber = phc.getString(phc.getColumnIndex(PHONE_NUMBER));
int phoneType = phc.getInt(phc.getColumnIndex(PHONE_TYPE));
phones.add(getPhoneTypeNameById(phoneType) + " : " + phoneNumber);
}
// 别忘记了把结合放到contactBean中!
contactBean.setNumbers(phones);// 搞定!
phc.close();
}
//Log.i(TAG, "phones: " + phones);
//获取邮箱
Cursor emc = resolver.query(EMAIL_URI,null, selection, null, null);
while (emc.moveToNext()) {
String emailData = emc.getString(emc.getColumnIndex(EMAIL_DATA));
int emailType = emc.getInt(emc.getColumnIndex(EMAIL_TYPE));
emails.add(getEmailTypeNameById(emailType) + " : " + emailData);
}
// 别忘记了把结合放到contactBean中!
contactBean.setEmail(emails);
emc.close();
//Log.i(TAG, "emails: " + emails);
// 最后把 contactBean 放到它的结合中就行了!
contactBeans.add(contactBean);
}
c.close();
return contactBeans;// 返回联系人信息!
}
private static String getPhoneTypeNameById(int typeId) {
switch (typeId) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: return "home";
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: return "mobile";
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: return "work";
default: return "none";
}
}
private static String getEmailTypeNameById(int typeId) {
switch (typeId) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME: return "home";
case ContactsContract.CommonDataKinds.Email.TYPE_WORK: return "work";
case ContactsContract.CommonDataKinds.Email.TYPE_OTHER: return "other";
default: return "none";
}
}
public static void writeContacts(Context context,String displayName,String phone,String email,String homePhone) throws RemoteException, OperationApplicationException{
/* 往 raw_contacts 中添加数据,并获取添加的id号*/
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
long contactId = ContentUris.parseId(resolver.insert(uri, values));
/* 往 data 中添加数据(要根据前面获取的id号) */
// 添加姓名
uri = Uri.parse("content://com.android.contacts/data");
values.put("raw_contact_id", contactId);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data2", displayName);
resolver.insert(uri, values);
// 添加电话
values.clear();
values.put("raw_contact_id", contactId);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data2", "2");
values.put("data1", phone);
resolver.insert(uri, values);
// 添加家庭电话号码,其它的操作, 比如家庭email 等, 都类似,就是
// data2数值不同罢了!
values.clear();
values.put("raw_contact_id", contactId);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data2", "1");
values.put("data1", homePhone);
resolver.insert(uri, values);
// 添加Email
values.clear();
values.put("raw_contact_id", contactId);
values.put("mimetype", "vnd.android.cursor.item/email_v2");
values.put("data2", "2");
values.put("data1", email);
resolver.insert(uri, values);
}
}