Android学习之调用系统联系人页面返回联系人信息

权限:


Intent intent = new Intent();

intent.setAction(Intent.ACTION_PICK);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);

startActivityForResult(intent, 1);

@Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
       if (resultCode == Activity.RESULT_OK) {
ContentResolver contentResolver = getContentResolver();
           Uri contactData = data.getData();
           // 获取联系人姓名,id
           Cursor cursor = getContentResolver().query(contactData, null, null, null, null);
           cursor.moveToFirst();
           String username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
           String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
           cursor.close();
           // 根据id获取联系人电话
           Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                   null,
//                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
                   "_id = " + contactId,
                   null,
                   null,
                   null);
           String usernumber1 = null;
           while (phoneCursor.moveToNext()) {
usernumber1 = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
           }
phoneCursor.close();
           showToast(username + "—-" + usernumber1);
       }
}

查询所有联系人

Uri uri = Uri.parse("content://icc/adn");
Cursor phone = reContentResolverol.query(uri,
       null,
       null,
       null,
       null);
while (phone.moveToNext()) {
int numberIndex = phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
   if(numberIndex == -1){
numberIndex = phone.getColumnIndex("number"); // Android2.2
   }
String phoneNumber = phone.getString(numberIndex);
   showToast(username + "—-" + phoneNumber);
}