权限
操作通讯录必须在AndroidManifest.xml中先添加2个权限,6.0之后动态权限请自行添加
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
增:
/**
* 添加 通讯录联系人
* @param context 上下文
* @param name 名称
* @param phoneNum 手机号
*/
public static void addContact(Context context, String name, String phoneNum) {
ContentResolver contentResolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
Uri mUri = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, contentValues);
long rawContactId = ContentUris.parseId(mUri);
//往data表入姓名数据
contentValues.clear();
contentValues.put(ContactsContract.RawContacts.Data.RAW_CONTACT_ID, rawContactId);
contentValues.put(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, name);
contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, contentValues);
//往data表入电话数据
contentValues.clear();
contentValues.put(android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId);
contentValues.put(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
contentValues.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneNum);
contentValues.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
contentResolver.insert(android.provider.ContactsContract.Data.CONTENT_URI, contentValues);
//插入人名
// values.clear();
// values.put(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, raw_contact_id);
// values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "小李");
// values.put(ContactsContract.CommonDataKinds.StructuredName.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
// contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
// //插入电话信息
// values.clear();
// values.put(ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID, raw_contact_id);
// values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, 135646461);
// values.put(ContactsContract.CommonDataKinds.Phone.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
// contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, values);
}
删:
/**
* 根据 名称 手机号 删除通讯录
* @param context 上下文
* @param name 名称
* @param number 手机号
*/
public static void delete(Context context, String name, String number) {
ContentResolver resolver = context.getContentResolver();
Cursor nameCursor = resolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[]{name}, null);
while (nameCursor.moveToNext()) {
//名称
String nameStr = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
//raw_id
String raw_id = nameCursor.getString(nameCursor.getColumnIndex(ContactsContract.RawContacts._ID));
//根据raw_id 查询手机号
Cursor numberCursor = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.RAW_CONTACT_ID + "=?", new String[]{raw_id}, null);
while (numberCursor.moveToNext()) {
String phone = numberCursor.getString(numberCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
phone = phone.replace("-", "");
phone = phone.replace(" ", "");
//判断
if (phone.equals(number)) {
//开始删除
int delete = resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts._ID + "=?", new String[]{raw_id});
}
}
}
}
改:
/**
* 修改联系人
*/
public void upPhoneMessage(Context context, PhoneMessageMode phoneMessageMode) {
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phoneMessageMode.getPhoneNumber());
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
String where = ContactsContract.Data.RAW_CONTACT_ID + "=? AND " + ContactsContract.Data.MIMETYPE + "=?";
String[] selectionArgs = new String[]{phoneMessageMode.getPhoneId(), ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE};
int res = context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, where, selectionArgs);
values.clear();
String[] selectionArgs1 = new String[]{phoneMessageMode.getPhoneId(), ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};
values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, phoneMessageMode.getPhoneName());
values.put(ContactsContract.RawContacts.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
context.getContentResolver().update(ContactsContract.Data.CONTENT_URI, values, where, selectionArgs1);
}
查:
/**
* 查询电话信息
*/
public static List<PhoneMessageMode> getPhoneMessage(Context context) {
//PhoneMessageMode为存放信息的mode类
List<PhoneMessageMode> phoneList = new ArrayList<>();
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int idInedx = cursor.getColumnIndex(ContactsContract.RawContacts.Data.RAW_CONTACT_ID);
int numberInedx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int nameIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
String phoneId;
String phoneNumber;
String phoneName;
while (cursor.moveToNext()) {
phoneId = cursor.getString(idInedx);//电话Id
phoneNumber = cursor.getString(numberInedx);//电话号码
phoneName = cursor.getString(nameIndex);//姓名
PhoneMessageMode phoneMessageMode = new PhoneMessageMode(phoneId, phoneNumber, phoneName);
phoneList.add(phoneMessageMode);
}
return phoneList;
}