调用系统联系人列表
Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, ACTION_PICK);
获取返回
代码
1 @Override
2 protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
3 if (ACTION_PICK == requestCode) {
4 if (resultCode == Activity.RESULT_OK) {
5 Uri contactData = intent.getData();
6 Cursor c = managedQuery(contactData, null, null, null, null);
7 if (c.moveToFirst()) {
8 getContactInfo(intent);
9 }
10 }
11 }
12 super.onActivityResult(requestCode, resultCode, intent);
13 }
解析联系人信息
代码
1 private void getContactInfo(Intent intent) {
2 Cursor cursor = managedQuery(intent.getData(), null, null, null, null);
3 while (cursor.moveToNext()) {
4 String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
5 String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
6 String phoneNumber = null, emailAddress=null;
7 String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
8
9 if (hasPhone.equalsIgnoreCase("1"))
10 hasPhone = "true";
11 else
12 hasPhone = "false";
13
14 if (Boolean.parseBoolean(hasPhone)) {
15 Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
16 ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
17 while (phones.moveToNext()) {
18 phoneNumber += phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)) +";";
19 }
20 phones.close();
21 }
22
23 txtContact.setText(name);
24 txtMessage.setText(phoneNumber);
25
26 Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
27 ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
28 while (emails.moveToNext()) {
29 emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
30 }
31 emails.close();
32
33 Cursor address = getContentResolver().query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
34 ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null);
35 while (address.moveToNext()) {
36 String poBox = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
37 String street = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
38 String city = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
39 String state = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
40 String postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
41 String country = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
42 String type = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
43 }
44 address.close();
45 }
46 cursor.close();
47 }