我在Android工作,我想知道如何找到电话号码的联系人姓名。
在BroadcastReceiver
我有这个:
String phonenr=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
我需要此电话号码的联系人姓名(全名 - 姓名+姓氏,如果有)。
THX!
答案 0 :(得分:1)
您可以尝试此link
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)
使用下面的函数:
Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("your receive phone number"));
Cursor phones = getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
while (phones.moveToNext())
{
//it returns contact name
String name=phones.getString(phones.getColumnIndex(PhoneLookup.DISPLAY_NAME));
}
答案 1 :(得分:0)
查看Retrieving Contact Information
//query for the people in your address book
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");
startManagingCursor(cursor);
//bind the name and the number fields
String[] columns = new String[] { People.NAME, People.NUMBER };
int[] to = new int[] { R.id.name_entry, R.id.number_entry };
SimpleContactAdapter mAdapter = new SimpleContactAdapter(this, R.layout.list_entry, cursor, columns, to);
this.setListAdapter(mAdapter);