2

I have built an app where I loop through and collect the users phone contacts, my aim is to then use these numbers and query my parse database and look for records that contain the users contacts (this will be to check if any of the users contacts are a user of my app, a users phone number will be saved to my parse database when they register). The problem I've got is that when collecting the users contacts numbers they are returned in different formats, some +447966000000, some 07966000000, some 07 966000 000000, etc.

My question is, what would be the best way to format my numbers when saving them to the database and retrieving them from the users contacts so that all numbers are saved and retrieved in the same format so that when I do a conditional check on them they will be easy to compare?

I have downloaded phone Number Utils library but I am not sure what in the library could be used to do something like this.



Code so far:

 Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            Toast.makeText(getApplicationContext(),name + " " + phoneNumber, Toast.LENGTH_LONG).show();
        }
        phones.close();
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34
Paul Alexander
  • 2,686
  • 4
  • 33
  • 69
  • Check this : [http://stackoverflow.com/questions/2543938/how-to-split-mobile-number-into-country-code-area-code-and-local-number](http://stackoverflow.com/questions/2543938/how-to-split-mobile-number-into-country-code-area-code-and-local-number) – Krishna Chandran Nov 06 '15 at 11:04
  • Possible duplicate of [How to know SMS number and phone number is the same?](http://stackoverflow.com/questions/17608803/how-to-know-sms-number-and-phone-number-is-the-same) – QuestionC Nov 06 '15 at 16:00

2 Answers2

6

You can use PhoneNumberUtils.compare to compare and check if they are same or not.It returns true if they are same ignoring country codes etc.

Example:

PhoneNumberUtils.compare(context, 1234567890, +911234567890);

returns true

A Nice Guy
  • 2,676
  • 4
  • 30
  • 54
0

I have done it for Indian mobile number format

   private String getNumber(String moNumber) {
    Pattern special = Pattern.compile ("[!@#$%&*()_+=|<>?{}\\[\\]~-]");
    if (moNumber.isEmpty()||moNumber.length()<10) {
        MydebugClass.showToast(getContext(), "Please input valid Number");
        return null;
    }else if (moNumber.length()>10 && !special.matcher(moNumber).find()){
        String[] number=moNumber.split("");
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=moNumber.length();i>moNumber.length()-10;i--){
            stringBuilder.append(number[i]);
        }
        String reverse=new StringBuffer(stringBuilder).reverse().toString();
        return reverse;
    }else if(moNumber.length()>10&&special.matcher(moNumber).find()){
        String numberOnly= moNumber.replaceAll("[^0-9]", "");
        String[] number=numberOnly.split("");
        StringBuilder stringBuilder=new StringBuilder();
        for(int i=moNumber.length();i>moNumber.length()-10;i--){
            stringBuilder.append(number[i]);
        }
        String reverse=new StringBuffer(stringBuilder).reverse().toString();
        Log.d("mobilenumberspecial",reverse);
        return reverse;
    }
    else {
        return moNumber;
    }
    return  null;
}