如何从android的联系人姓名中获取联系人的号码?
16lz
2021-01-22
I am having a contacts name with me and want his number .How to get contact number of corresponding name in Android ?
我有一个联系人的名字,想要他的号码,如何在Android中获得相应名字的联系电话?
3 个解决方案
#1
17
A shorter version; you still need that permission (android.permission.READ_CONTACTS)
较短的版本;您仍然需要这个权限(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;
}