[英]How to get the details of a particular contact from the phone contact list?
我想做的是,我将提供一个电话号码,我想获取将该电话号码存储在移动联系人列表中的名称。 我尝试了来自不同站点的许多代码,但没有一个可以满足我的要求! 由于许多小时的任何帮助,我们将不胜感激。 提前致谢!
目前,我正在使用此代码:
Uri uri = Uri.withAppendedPath(Phones.CONTENT_FILTER_URL, Uri.encode(incomingNumber));
Toast.makeText(context, incomingNumber, Toast.LENGTH_LONG).show();
String name = null;
Cursor cursor = context.getContentResolver().query(uri,new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }, null, null, null);
Toast.makeText(context, p.getString(cursor.getString(0), "unknown"), Toast.LENGTH_LONG).show();
// Invoke endCall()
if (cursor != null && cursor.moveToFirst()) {
editor1.putBoolean("fromcontacts", true);
editor1.putBoolean("notfromcontacts", false);
editor1.putString("incomingnumbername", cursor.getString(0));
editor1.commit();
// Toast.makeText(context, p.getString("incomingnumbername", "unknown"), Toast.LENGTH_LONG).show();
}
else
{
editor1.putBoolean("notfromcontacts", true);
editor1.putBoolean("fromcontacts", false);
editor1.putString("incomingnumbername", "Unknown");
editor1.commit();
Toast.makeText(context, p.getString("incomingnumbername", "unknown"), Toast.LENGTH_LONG).show();
}
您可以使用此代码获取所有联系人,然后根据给定的电话号码循环搜索并检查姓名
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(NativeContentProvider.this, "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();
}
pCur.close();
}
}
}
希望能帮助到你 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.