A
A
Anton Akimov2017-11-24 13:45:11
Android
Anton Akimov, 2017-11-24 13:45:11

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

1 answer(s)
A
Anton Akimov, 2017-11-24
@antaki93

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;

Source: https://stackoverflow.com/questions/6330151/how-to...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question