//打开手机通讯录
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
//获取通讯录数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case (1): {
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
c.moveToFirst();
String phoneNum = ContactsUtil.getContactPhone(c, getApplicationContext());
et_phone.setText(phoneNum);
if (TextUtils.isEmpty(phoneNum)) {
return;
}
}
break;
}
}
}
//获取联系人电话
public class ContactsUtil {
public static String getContactPhone(Cursor cursor,Context context) {
int phoneNum = 0;
int phoneColumn = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
if (cursor.moveToFirst()) {
phoneNum = cursor.getInt(phoneColumn);
}
String phoneResult = "";
//System.out.print(phoneNum);
if (phoneNum > 0) {
// 获得联系人的ID号
int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
String contactId = cursor.getString(idColumn);
// 获得联系人的电话号码的cursor;
Cursor phones = context.getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
if (phones.moveToFirst()) {
// 遍历所有的电话号码
for (; !phones.isAfterLast(); phones.moveToNext()) {
int index = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int typeindex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
int phone_type = phones.getInt(typeindex);
String phoneNumber = phones.getString(index);
switch (phone_type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
case ContactsContract.CommonDataKinds.Phone.TYPE_CALLBACK:
case ContactsContract.CommonDataKinds.Phone.TYPE_CAR:
case ContactsContract.CommonDataKinds.Phone.TYPE_COMPANY_MAIN:
case ContactsContract.CommonDataKinds.Phone.TYPE_ISDN:
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER_FAX:
case ContactsContract.CommonDataKinds.Phone.TYPE_RADIO:
case ContactsContract.CommonDataKinds.Phone.TYPE_TELEX:
case ContactsContract.CommonDataKinds.Phone.TYPE_TTY_TDD:
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_PAGER:
case ContactsContract.CommonDataKinds.Phone.TYPE_ASSISTANT:
case ContactsContract.CommonDataKinds.Phone.TYPE_MMS:
phoneResult = phoneNumber;
break;
}
}
if (!phones.isClosed()) {
phones.close();
}
}
}
return phoneResult;
}
}
网友评论