开启通讯录
// 跳到通讯录
final int INTENT_CONTACT = 1000
Intent intent = new Intent()
intent.setAction(Intent.ACTION_PICK)
intent.setData(ContactsContract.Contacts.CONTENT_URI)
startActivityForResult(intent, INTENT_CONTACT)
获取信息
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
if (data == null) {
showToast("抱歉,您选择的联系人没有电话,请重新选择!");
return;
}
Uri uri = data.getData();
String id = uri.getLastPathSegment();
setPhoneNumsByContactId(id);
}
}
/** 存储选中用户名和手机 **/
private HashMap<String, String> phoneMap;
/**
* 设置选中联系人信息
*
* @param contactId
* 联系人ID
*/
public void setPhoneNumsByContactId(String contactId) {
Cursor phonecursor = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?",
new String[] { contactId }, null);
List<String> phoneNums = new ArrayList<String>();
List<String> phoneNames = new ArrayList<String>();
while (phonecursor.moveToNext()) {
String phoneNum = phonecursor
.getString(phonecursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String phoneName = phonecursor.getString(phonecursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (!TextUtils.isEmpty(phoneNum)) {
phoneNums.add(phoneNum);
phoneNames.add(phoneName == null ? "" : phoneName);
}
}
if (phoneNums.size() == 0) {
showToast("抱歉,您选择的联系人没有电话,请重新选择!");
} else if (phoneNums.size() > 0) {
name = phoneNames.get(0);
if (phoneMap == null) {
phoneMap = new HashMap<String, String>();
}
phoneMap.put(name, getNumber(phoneNums.get(0)));
}
}