quwuwuza
quwuwuza

Reputation: 394

android find correct phone number

Given a phone number as string, how do I find the correct value of it as stored in contacts?

Example:
Given Phone number: 9743343954
Phone number in contacts: +919743343954

Edit: The length of the numbers isn't fixed.

Thanks.

Upvotes: 1

Views: 2163

Answers (5)

Antoineguiral
Antoineguiral

Reputation: 11

The answer from @Lecho seems is good if you want to get the associated contact (or related information). More information on this page from android doc http://developer.android.com/reference/android/provider/ContactsContract.PhoneLookup.html

As far as I know, PhoneLookup.NORMALIZED_NUMBER is available since API 16.

On the other side, if you want to compare 2 numbers to see if they are the same but formated in two different ways you can use :

PhoneNumberUtils.compare(phoneNumber1, phoneNumber2)

Best,

Upvotes: 1

Leszek
Leszek

Reputation: 6598

I'm not sure I understand what you mean but maybe this will help you:

    //Find contact by given number
    Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode("9743343954"));
    String[] projection = new String[] { PhoneLookup.NUMBER, PhoneLookup.NORMALIZED_NUMBER };
    Cursor c = getContentResolver().query(uri, projection, null, null, null);
    if (c.moveToFirst()) {// while(c.moveToNext()){
        //get number assigned by user to given contact, in this case 9743343954
        String number = c.getString(c.getColumnIndexOrThrow(PhoneLookup.NUMBER));
        //get normalized E164 number, in this case +919743343954
        String normalized = c.getString(c.getColumnIndexOrThrow(PhoneLookup.NORMALIZED_NUMBER));
        Toast.makeText(getApplicationContext(), "Number: " + number + "; normalized: " + normalized,
                Toast.LENGTH_LONG).show();
    }
    c.close();

To make this works add permission to project manifest:

    <uses-permission android:name="android.permission.READ_CONTACTS" />

Upvotes: 4

nithinreddy
nithinreddy

Reputation: 6197

Why don't you do this

Assume the phone number given is

String a = "9123456789";

And the one in contacts is

String b = "+91-9123456789";

Then you can easily check it this way

if(b.contains(a))
{
//Do what you want!
}

Upvotes: 1

Karan_Rana
Karan_Rana

Reputation: 2823

First step would be to remove all extra spaces and extra characters:

//Removes the extra characters from the incoming number
    private String removeExtraCharacters(String phoneNumber){
        try
        {
            phoneNumber = phoneNumber.replace("(", "");
            phoneNumber = phoneNumber.replace(")", "");
            phoneNumber = phoneNumber.replace("-", "");
            phoneNumber = phoneNumber.replace(" ", "");
            phoneNumber = phoneNumber.replace("+", "");

        }
        catch (Exception e) 
        {
            Log.e("Cal Reciever_R", e.toString());
        }
        return phoneNumber;
    }

Now you can use the substring method to get 10 digit number and ignorin the +91 from front as follows:

phoneNumber =phoneNumber .substring(phoneNumber .length()-10);

Upvotes: -1

Neil Townsend
Neil Townsend

Reputation: 6084

Using your example, the phone number is of length 10 characters, in contacts it is 13, and you want the last 10 of those to match. So, something like:

// I am assuming that you've removed all spaces, full stops,
// dashes etc from the two input variables
public boolean numbersMatch(String numberToMatch, String numberFromContacts) {
    // Ensure that the both numbers are of a reasonable length
    if (numberToMatch.length() < 9) return false;
    if (numberFromContacts.length() < 9) return false;

    // Is the number to match hidden in the contacts number
    if (numberFromContacts.lastIndexOf(numberToMatch) != -1) return true;

    // Or is the contact number hidden in the number to macth
    if (numberToMatch.lastIndexOf(numberFromContacts) != -1) return true;

    // No match, so return false
    return false;
}

Upvotes: 2

Related Questions