user1165435
user1165435

Reputation: 231

How to obtain the name of a caller from the phone number in Android

I am working in Android and I would like to know how to find out the contact name of a phone number.

In the BroadcastReceiver I have this:

String phonenr=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);

I need the contact name ( full name - firstname + familyname , if there is) for this phone number.

Thx!

Upvotes: 1

Views: 769

Answers (2)

Dinesh
Dinesh

Reputation: 6532

You can Try this link

 Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
     resolver.query(uri, new String[]{PhoneLookup.DISPLAY_NAME} .....)

Use the function Below like this:

  Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("your receive phone number"));
  Cursor phones = getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);
        while (phones.moveToNext())
        {
            //it returns contact name
            String name=phones.getString(phones.getColumnIndex(PhoneLookup.DISPLAY_NAME));
        }

Upvotes: 1

user671253
user671253

Reputation:

Check this out Retrieving Contact Information

 //query for the people in your address book
    Cursor cursor = getContentResolver().query(People.CONTENT_URI, null, null, null,People.NAME + " ASC");
    startManagingCursor(cursor);

    //bind the name and the number fields
    String[] columns = new String[] { People.NAME, People.NUMBER };
    int[] to = new int[] { R.id.name_entry, R.id.number_entry };
    SimpleContactAdapter mAdapter = new SimpleContactAdapter(this, R.layout.list_entry, cursor, columns, to);
    this.setListAdapter(mAdapter);

Upvotes: 0

Related Questions