以下是根据一个已知的电话号码,从通讯录中获取相对应的联系人姓名的代码:
- public String getContactNameFromPhoneBook(Context context, String phoneNum) {
- String contactName = "";
- ContentResolver cr = context.getContentResolver();
- Cursor pCur = cr.query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
- ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?",
- new String[] { phoneNum }, null);
- if (pCur.moveToFirst()) {
- contactName = pCur
- .getString(pCur
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
- pCur.close();
- }
- return contactName;
- }