一,清單文件,添加權(quán)限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- 獲取用戶聯(lián)系人數(shù)據(jù) -->
二,動(dòng)態(tài)申請(qǐng)權(quán)限

三,跳轉(zhuǎn)系統(tǒng)聯(lián)系人界面
//權(quán)限申請(qǐng)成功后,打開通訊錄
public void openPhoneBook() {
Intent intent =new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
? ? startActivityForResult(intent, Constant.REQUEST_CODE_PHONE);
}
四,回調(diào)數(shù)據(jù)

//獲取到電話
private void getPhoneBookName(Intent data) {
if (data !=null) {
Uri uri = data.getData();
? ? ? ? String[] contact =new String[2];
? ? ? ? //得到ContentResolver對(duì)象**
? ? ? ? ContentResolver cr = getContentResolver();
? ? ? ? //取得電話本中開始一項(xiàng)的光標(biāo)**
? ? ? ? Cursor cursor=cr.query(uri,null,null,null,null);
? ? ? ? if(cursor!=null)
{
cursor.moveToFirst();
? ? ? ? ? ? String ContactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
? ? ? ? ? ? Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
? ? ? ? ? ? ? ? ? ? ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"=" + ContactId, null, null);
? ? ? ? ? ? if(phone !=null){
phone.moveToFirst();
? ? ? ? ? ? ? ? contact[0] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
? ? ? ? ? ? ? ? String phoneNum = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
? ? ? ? ? ? ? ? //? 把電話號(hào)碼中的? -? 符號(hào) 替換成空格
? ? ? ? ? ? ? ? phoneNum = phoneNum.replaceAll("-", " ");
? ? ? ? ? ? ? ? // 空格去掉? 為什么不直接-替換成"" 因?yàn)闇y(cè)試的時(shí)候發(fā)現(xiàn)還是會(huì)有空格 只能這么處理
? ? ? ? ? ? ? ? phoneNum = phoneNum.replaceAll(" ", "");
? ? ? ? ? ? ? ? contact[1] = phoneNum;
? ? ? ? ? ? }
phone.close();
? ? ? ? ? ? cursor.close();
? ? ? ? }
String contactName = contact[0]; //姓名
? ? ? ? String phoneNum = contact[1];//電話
? ? }
}