添加权限
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
Android6.0获取手机通讯录动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getBaseActivity(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED)
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 1001);
}
获取权限回调
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1001:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
break;
}
}
读取手机联系人通讯录
public class ContactUtils {
public static List<PhoneInfo> getAllContacts(Context context) {
List<PhoneInfo> phoneInfoList = new ArrayList<PhoneInfo>();
ContentResolver resolver = context.getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Pohne.CONTENT_URI;
String[] projection = new String[]{"contact_id", "display_name", "sort_key", "version"};
if(Build.VERSION.SDK.INT >= 19{
projection[2] = "phonebook_label"
Cursor testCursor = null;
try{
testCursor = resolver.query(uri, new String[]{"phonebook_label", null, null, null};
} catch (Exception e){
projection[2] = "sort_key";
} finally {
if(testCursor != null)
testCursor.close();
}
}
Cursor cursor = resolver.query(uri, projection, null, null, projection[2]);
if (cursor != null) {
while (cursor.moveToNext()) {
PhoneInfo phoneInfo = new PhoneInfo();
List<String> telPhone = new ArrayList<>();
int id = cursor.getInt (cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String sortKey = getSortKey(cursor.getString(2));
long contactVersion = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts.VERSION));
String[] phoneProjection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
String contact_id = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id;
Cursor cursorPhone = resolver.query(uri, phoneProjection, contact_id, null, null);
if (cursorPhone != null) {
while (cursorPhone.moveToNext()) {
String phone = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
telPhone.add(phone);
}
}
phoneInfo.setName(name);
phoneInfo.setSortKey(sortKey);
phoneInfo.setTelPhoneList(telPhone);
phoneInfoList.add(phoneInfo);
}
}
return phoneInfoList;
}
private static String getSortKey(String sortKeyString) {
String key = sortKeyString.substring(0, 1).toUpperCase();
if (key.matches("[A-Z]")) {
return key;
} else {
return "#";
}
}
}
共享数据监听
public class ContactContentObserver extends ContentObserver {
public ContactContentObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.d("abcd","联系人列表发生变化");
}
}
注册监听通讯录变化
private void myObserver() {
observer = new ContactContentObserver(new Handler());
resolver = getBaseActivity().getContentResolver();
Uri uri = ContactsContract.Contacts.CONTENT_URI;
resolver.registerContentObserver(uri, true, observer);
}
取消注册监听
@Override
protected void onDestroy() {
resolver.unregisterContentObserver(observer);
}