Created
November 1, 2018 14:21
-
-
Save WahdanZ/02629dbd983d951f6eb1cd0387a33b76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PhoneUtils { | |
public static final String TAG = "PhoneUtils"; | |
private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); | |
public static Phonenumber.PhoneNumber getValidPhoneNumber(String countryCode, String phoneNumberStr) { | |
return getValidPhoneNumber(countryCode.concat(phoneNumberStr)); | |
} | |
public static String getValidPhoneNumberStr(String countryCode, String phoneNumberStr) { | |
Phonenumber.PhoneNumber phoneNumber = getValidPhoneNumber(countryCode.concat(phoneNumberStr)); | |
return phoneNumber.getCountryCode() + "-" + phoneNumber.getNationalNumber(); | |
} | |
public static Phonenumber.PhoneNumber getValidPhoneNumber(String phoneNumberStr) { | |
try { | |
Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneNumberStr, ""); | |
if (phoneNumberUtil.isValidNumber(phoneNumber)) { | |
return phoneNumber; | |
} | |
} catch (NumberParseException e) { | |
e.printStackTrace(); | |
return null; | |
} | |
return null; | |
} | |
public static PhoneNumberUtil.PhoneNumberType PhoneNumberType(Phonenumber.PhoneNumber phoneNumber){ | |
return phoneNumberUtil.getNumberType(phoneNumber); | |
} | |
public static String getPhoneNumberCountyCode(Phonenumber.PhoneNumber phoneNumber) { | |
return phoneNumberUtil.getRegionCodeForNumber(phoneNumber); | |
} | |
public static String getNationalNumber(Phonenumber.PhoneNumber phoneNumber) { | |
return phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL).replace(" ", ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment