简体   繁体   中英

Android Eclipse incoming vs outgoing SMS: Phone number format?

I'm working on a application that captures the in/outgoing SMS on Android devices.

Now since I'm already in the beta release, I noticed on my SQL server that the incoming SMS adress is formatted as an international number, like +32477889977.

However, the outgoing SMS gets formatted like 0477 88 99 77, the way the user stores it on his phone.

My code for outgoing SMS is

int addressColumn = cursor.getColumnIndex("address");
String number = cursor.getString(addressColumn);

But in the incoming monitor the code is

String from = msg[i].getOriginatingAddress();

Now the question is, how can I make the incoming SMS to have the same format as the outgoing, so the international number, and not the number stored in the phone?

It is vital for my application that the phone numbers are international, since I target a worldwide application. So local formatting is not an option. I really need to read the number that was sent by the network.

You should use E164 numbers format. E164 is a standard for phone numbers formatting, it takes into account the locale that you are working on.

Google has their E164 library: https://code.google.com/p/libphonenumber/


an example of formatting a number in E164 using the library will look something like:

    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    PhoneNumber phoneNumber;
    try {
        phoneNumber = phoneUtil.parse(phone, locale.getCountry());
    } catch (NumberParseException e) {
        throw new InvalidNumberException(convertErrorType(e.getErrorType()), e.getMessage(), e);
    }
    String formatedMsisdn = phoneUtil.format(phoneNumber, PhoneNumberFormat.E164);


When you receive an SMS the number is already e164 formatted so you will get the same number when using the lib, but when you send an SMS you should first format it using a code such as above to get it in E164 format.
The locale you use is typically the locale the device is set to, you can get it by calling:

Locale.getDefault()

then passing the locale to the E164 library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM