废话少说,直接上代码
/**
* 添加姓名、电话号码到系统联系人
*
* @param context 上下文
* @param contactName 姓名
* @param phoneNumber 电话号码
*/
public static void addContact(Context context, String contactName, String phoneNumber) {
//先查询要添加的号码是否已存在通讯录中, 不存在则添加. 存在则提示用户
Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNumber);
ContentResolver resolver = context.getContentResolver();
//从raw_contact表中返回display_name
Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME}, null, null, null);
if (cursor == null)
return;
if (cursor.moveToFirst()) {
ToastUtils.showToast("存在相同号码");
} else {
// 创建一个空的ContentValues
ContentValues values = new ContentValues();
// 向RawContacts.CONTENT_URI空值插入,先获取Android系统返回的rawContactId,后面要基于此id插入值
Uri rawContactUri = context.getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//todo 添加姓名
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);// 内容类型
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contactName);// 联系人名字
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);//添加数据
//todo 添加电话
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNumber);// 联系人的电话号码
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);//电话类型
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);//添加数据
//todo 添加Email邮件
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Email.DATA, "zhangphil@xxx.com");// 联系人的地址
values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);//电子邮件的类型
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);//添加数据
//todo 添加组织机构名称
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Organization.DATA4, "产品经理");//职务
values.put(ContactsContract.CommonDataKinds.Organization.DATA1, "安荣科技有限公司");//公司
context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);//添加数据
ToastUtils.showToast("添加联系人成功");
}
cursor.close();
}
/**
* 删除联系人
*/
public static void deleteContact(Context context, String name) {
//根据姓名求id
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, "display_name=?", new String[]{name}, null);
if (cursor == null)
return;
if (cursor.moveToFirst()) {
int id = cursor.getInt(0);
//根据id删除data中的相应数据
resolver.delete(uri, "display_name=?", new String[]{name});
uri = Uri.parse("content://com.android.contacts/data");
resolver.delete(uri, "raw_contact_id=?", new String[]{id + ""});
Toast.makeText(context, "删除号码成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "没有找到号码", Toast.LENGTH_SHORT).show();
}
cursor.close();
}
/**
* 更改联系人
*/
public static void changeContact(Context context, String name, String newPhone) {
//根据姓名求id
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{ContactsContract.Data._ID}, "display_name=?", new String[]{name}, null);
if (cursor == null)
return;
if (cursor.moveToFirst()) {
int id = cursor.getInt(0);
ContentValues values = new ContentValues();
values.put("data1", newPhone);
resolver.update(uri, values, "mimetype=? and raw_contact_id=?", new String[]{"vnd.android.cursor.item/phone_v2", id + ""});
} else {
Toast.makeText(context, "没有找到号码", Toast.LENGTH_SHORT).show();
}
cursor.close();
}
/**
* 查询手机联系人
*/
public static void queryContactsShowData(Context context) {
List<HashMap<String, String>> mContactList = new ArrayList<>();
mContactList.clear();
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor == null)
return;
while (cursor.moveToNext()) {
String phoneName;
String phoneNumber;
HashMap<String, String> listItem = new HashMap<>();
phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
listItem.put("phoneName", phoneName);
listItem.put("phoneNumber", phoneNumber);
mContactList.add(listItem);
}
cursor.close();
}
搞定!