思路:
1.点击跳转到通讯录界面
`<pre>
Uri uri = Uri.parse("content://contacts/people");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
startActivityForResult(intent, 0);
</pre>`
2.获取通讯录姓名和手机号码
<pre>
/* * 跳转联系人列表的回调函数 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode){
case 0:
if(data==null) { return; }
//处理返回的data,获取选择的联系人信息
Uri uri=data.getData(); String[] contacts=getPhoneContacts(uri);
et_name.setText(contacts[0]);
et_tele.setText(contacts[1]);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private String[] getPhoneContacts(Uri uri){
String[] contact=new String[2];
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor=cr.query(uri,null,null,null,null);
if(cursor!=null) {
cursor.moveToFirst();
//取得联系人姓名
int nameFieldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
contact[0]=cursor.getString(nameFieldColumnIndex);
//取得电话号码
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[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phone.close();
cursor.close();
} else {
return null;
}
return contact;
}
</pre>
3.回调显示姓名和手机号码
4.别忘了加权限
<pre>
<!--获取通讯录权限-->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
</pre>