Answer the question
In order to leave comments, you need to log in
How to get phone number from contact name?
How to get the main phone number in the phone book by contact name ? Within RuNet there are solutions for similar (and even more complex) tasks, but I could not find a simple solution for this simple task.
Answer the question
In order to leave comments, you need to log in
A working and compact solution found on the English StackOverflow.
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question