Skip to content

Instantly share code, notes, and snippets.

@iniyanmurugavel
Created May 16, 2020 12:23
Show Gist options
  • Select an option

  • Save iniyanmurugavel/d99406671d5822fe6fd90a39cf4a0cff to your computer and use it in GitHub Desktop.

Select an option

Save iniyanmurugavel/d99406671d5822fe6fd90a39cf4a0cff to your computer and use it in GitHub Desktop.
Please use this function to pass phone number and country code like india 91
public static String parseContact(String contact, String countrycode) {
PhoneNumber phoneNumber = null;
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
String finalNumber = null;
String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countrycode));
boolean isValid = false;
PhoneNumberType isMobile = null;
try {
phoneNumber = phoneNumberUtil.parse(contact, isoCode);
isValid = phoneNumberUtil.isValidNumber(phoneNumber);
isMobile = phoneNumberUtil.getNumberType(phoneNumber);
} catch (NumberParseException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
if (isValid
&& (PhoneNumberType.MOBILE == isMobile || PhoneNumberType.FIXED_LINE_OR_MOBILE == isMobile)) {
finalNumber = phoneNumberUtil.format(phoneNumber,
PhoneNumberFormat.E164).substring(1);
}
return finalNumber;
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
// phone must begin with '+'
PhoneNumber numberProto = phoneUtil.parse(phone, "");
int countryCode = numberProto.getCountryCode();
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
// useful commands
keytool -list -v -keystore my-release-key.keystore
I use the google library https://code.google.com/p/libphonenumber/.
// useful links
https://mindorks.com/android/store/Utils/michaelrocks/libphonenumber-android
https://mindorks.com/android/store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment