如何从android中的联系人姓名获取联系人的号码
Posted
技术标签:
【中文标题】如何从android中的联系人姓名获取联系人的号码【英文标题】:How to get a contact's number from contact name in android 【发布时间】:2011-09-13 21:09:38 【问题描述】:我有一个联系人姓名,想要他的号码。如何在 android 中获取对应姓名的联系人号码?
【问题讨论】:
这些是一些对您有用的链接。 ***.com/questions/2356084/…***.com/questions/866769/… 【参考方案1】:较短的版本; 您仍然需要该权限 (android.permission.READ_CONTACTS)
public String getPhoneNumber(String name, Context context)
String ret = null;
String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" like'%" + name +"%'";
String[] projection = new String[] ContactsContract.CommonDataKinds.Phone.NUMBER;
Cursor c = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, selection, null, null);
if (c.moveToFirst())
ret = c.getString(0);
c.close();
if(ret==null)
ret = "Unsaved";
return ret;
【讨论】:
如何从我的main
活动中调用它?
String number=getPhoneNumber(contactName,MainActivity.this);【参考方案2】:
以下代码将注销以记录显示名称为contactName的联系人的所有手机号码:
Cursor cursor = null;
try
cursor = getContentResolver().query(Data.CONTENT_URI,
new String [] Data.RAW_CONTACT_ID ,
StructuredName.DISPLAY_NAME + "=? AND "
+ Data.MIMETYPE + "='" + StructuredName.CONTENT_ITEM_TYPE + "'",
new String[] contactName, null);
if (cursor != null && cursor.moveToFirst())
do
String rawContactId = cursor.getString(0);
Cursor phoneCursor = null;
try
phoneCursor = getContentResolver().query(Data.CONTENT_URI,
new String[] Data._ID, Phone.NUMBER,
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Phone.TYPE + "=" + Phone.TYPE_MOBILE + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] rawContactId, null);
if (phoneCursor != null && phoneCursor.moveToFirst())
String number = phoneCursor.getString(phoneCursor.getColumnIndex(Phone.NUMBER));
Log.d(TAG, "Mobile Number: " + number);
finally
if (phoneCursor != null)
phoneCursor.close();
while (cursor.moveToNext());
finally
if (cursor != null)
cursor.close();
【讨论】:
您好,我尝试了类似的方法,但没有成功。这是我的问题,非常感谢您的帮助! :) ***.com/questions/35097844/get-contact-name/…【参考方案3】:试试这个代码
people = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, "person_name"+"='"+name+"'", null, null);
people.moveToFirst();
try
String contactId = people.getString(people.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = people.getString(people.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if ( hasPhone.equalsIgnoreCase("1"))
hasPhone = "true";
else
hasPhone = "false" ;
if (Boolean.parseBoolean(hasPhone))
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
while (phones.moveToNext())
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
mConno.add(position,phoneNumber);
phones.close();
if(hasPhone=="false")
mConname.remove(position);
else
position++;
catch(Exception e)
【讨论】:
P.S.您还必须拥有查询联系人的权限。 是的,你需要以上是关于如何从android中的联系人姓名获取联系人的号码的主要内容,如果未能解决你的问题,请参考以下文章