Unique Contacts By Phone Number Android
How to filter contacts by mobile number like chat applications. If a mobile number saved in contacts database with twice by with country code and without country code. I need to me
Solution 1:
You need to convert all phone numbers to E164 format (with country code, no spaces or dashes) e.g: +12125551234
If you're targeting API level 21 and above, you can simply do:
Stringe164= PhoneNumberUtils.formatNumberToE164(originalNumber, countryCode);
To get the country code, try this:
TelephonyManagertelephony= (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
StringcountryCode= telephony.getSimCountryIso().toUpperCase();
If you're targeting api level below 21, you can use google' libphonenumber library for Android with a similar API.
If all phones are normalized into E164 format, you can remove duplicates by simply not adding a new phone to a contact in case that contact already has that phone (you might find HashMap useful for this)
Post a Comment for "Unique Contacts By Phone Number Android"