Formatting phone number to E164 format in Android -
i want convert every phone number conatct in device e164 format. so, used opensource below.
so used here.
phonenumber.phonenumber formattednumber = null; string formatted = null; try { formattednumber = phoneutil.parse(phonenumber, "kr"); formatted = phoneutil.format(formattednumber,phonenumberutil.phonenumberformat.e164); if (stringutils.isempty(formatted) == false && formatted.length() > 0 && stringutils.isempty(name) == false && name.length() > 0) { listname.add(name); listphonenumber.add(formatted); } } catch (numberparseexception e) { continue; }
and read library used android framework since 4.0.
the java version optimized running on smartphones, , used android framework since 4.0 (ice cream sandwich).
i want use android sdk. found this. android sdk provides phonenumberutils .
and there function
formatnumbertoe164(string phonenumber, string defaultcountryiso)
it's easy use. api level of function 21.
so question is..how can use phonenumberutils convert phonenumber e164 under api level 14(ics) ~ 21?
thanks.!
the problem phonenumberutils.formatnumbertoe164(...)
not available on older devices , there's nothing else in phonenumberutils same job.
i'd suggest using phonenumberutils when available , libphonenumber on older devices, example:
public string formate164number(string countrycode, string phnum) { string e164number; if (textutils.isempty(countrycode)) { e164number = phnum; } else { if (build.version.sdk_int >= build.version_codes.lollipop) { e164number = phonenumberutils.formatnumbertoe164(phnum, countrycode); } else { try { phonenumberutil instance = phonenumberutil.getinstance(); phonenumber.phonenumber phonenumber = instance.parse(phnum, countrycode); e164number = instance.format(phonenumber, phonenumberutil.phonenumberformat.e164); } catch (numberparseexception e) { log.e(tag, "caught: " + e.getmessage(), e); e164number = phnum; } } } return e164number; }
Comments
Post a Comment