1.启动系统选择联系人界面
Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
2.重写OnActivityResult方法获取数据
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(data!=null){
switch (requestCode)
{
case 0:
Uri conatctData = data.getData();
Cursor cursor = managedQuery(conatctData, null, null, null, null);
if (cursor.moveToFirst()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String names = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)) ;
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"="+contactId, null, null);
String phonenum="";
if (phones.moveToFirst()) {
phonenum = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
name.setText(names);
phone.setText(phonenum);
if (Build.VERSION.SDK_INT <14) {
phones.close();
}
}
if (Build.VERSION.SDK_INT <14) {//不添加的话Android4.0以上系统运行会报错
cursor.close();
}
break;
}
}
}
3.添加权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>