Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Contact from number with Country Code

When retrieving a mobile number from an incoming sms it has the country code appended to it.
Now as the matching contact in the database may not include the country code to get the contact name I'm using:

public static String getContactName(String number, Context context) {

    try {

        Uri personUri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number.trim()));
        String selection = PhoneLookup.NUMBER + " LIKE %" + number.trim() + "%";
        Cursor cur = context.getContentResolver().query(personUri,
            new String[] {
            PhoneLookup.DISPLAY_NAME
        },
            selection, null, null);
        if (cur.moveToFirst()) {
            String str = cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME));
            cur.close();
            return str;
        }
    } catch (Exception e) {
        //e.printStackTrace();
        return number;
    }
    return number;
}

This works perfectly if I pass in an exact match or a partial match. However if I want to get the contact id I'm using this code:

public static Contact getContact(String number, ContentResolver contentResolver) {

    Contact contact = new Contact(-1, number);
    try {
        Uri personUri = Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_FILTER_URI, Uri.encode(number));
        String selection = ContactsContract.CommonDataKinds.Phone.NUMBER + " LIKE '%" + number + "%'";
        Cursor cur = contentResolver.query(personUri,
            new String[] {
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID, PhoneLookup.DISPLAY_NAME
        },
            selection, null, null);
        if (cur.moveToFirst()) {
            contact = new Contact(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID))),
                cur.getString(cur.getColumnIndex(PhoneLookup.DISPLAY_NAME)));
        }
        cur.close();
    } catch (Exception e) {}
    return contact;
}

This works perfect if the number is an exact match but if it is not it returns nothing. I need this method to find the id of the contact where its number is saved as "01111111111" and the incoming number is "+441111111111". Although they are looking at different URI's the selection code is pretty much the same so I'm not sure why it doesn't work for the second.

Any ideas?

like image 939
crazyfool Avatar asked Apr 24 '13 11:04

crazyfool


People also ask

How do you enter a phone number with a country code?

To call a phone in another country, dial 011, and then the code for the country you are calling, the area or city code, and the phone number. For example, if you are trying to call someone in Brazil (country code 55), in the city of Rio de Janeiro (city code 21), you would dial 011 - 55 - 21 - XXXX-XXXX.

How do I fix country code in contacts?

1) Go to Settings > Accounts > remove the Google account > add it back. 2) Go to Gmail on a desktop > Go to Settings (the gear icon in the top right) > Under General, go to 'Phone Numbers' and select your default country code from the dropdown list.


1 Answers

I am using this code for the same requirement and it returns a name by returning contactId from the same code. Using this, your problem will be solved.

public static String getContactDisplayNameByNumber(String number) {
    Uri uri = Uri.withAppendedPath(
            ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
            Uri.encode(number));
    String name = "?";

    ContentResolver contentResolver = context.getContentResolver();
    Cursor contactLookup = contentResolver.query(uri, new String[] {
            BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
            null, null, null);

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

    return name;
}
like image 64
AMD Avatar answered Sep 19 '22 13:09

AMD