user2569548
user2569548

Reputation: 41

get the Display name of incoming calling number

To get the incoming calling number we use

  TeleponyManager.EXTRA_INCOMING_NUMBER

but how to get the display name of incoming call when it is already saved in contact database .

Upvotes: 0

Views: 2056

Answers (2)

user5723719
user5723719

Reputation:

create a method called getContactDisplayNamebyNumber in your broadcast receiver and then pass that incoming number as parameter in the method . the method will check whether the number is saved with contact name or not in your mobile if yes it will return the contact name otherwise it will return the unknown number

check out this code

   public String getContactDisplayNameByNumber(String number,Context context) {
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    name = "Incoming call from";

    ContentResolver contentResolver = context.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, null, null, null, null);

    try {
        if (contactLookup != null && contactLookup.getCount() > 0) {
            contactLookup.moveToNext();
            name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
            // this.id =
            // contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.CONTACT_ID));
            // String contactId =
            // contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
        }else{
            name = "Unknown number";
        }
    } finally {
        if (contactLookup != null) {
            contactLookup.close();
        }
    }

    return name;
}

get code from Vinod Dirishala's GitHub Repository

Happy coding :D :D

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93542

  Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(newSender));
  Cursor cursor = getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME}, newSender, null, null );
  if(cursor.moveToFirst()){
        newSender =      cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
  }
  cursor.close();

Where newSender is the incoming telephone number

Upvotes: 3

Related Questions