Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

java - Non english SMS appears as multiple strings?

I am reading text messages in my application. Whenever an SMS arrives it comes to the app and is displayed.

It is working for English but when in the Gujarati language they're broken into more than one string.

Here's my code:

final Object[] pdusObj = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdusObj.length];
                for (int i=0; i<msgs.length; i++) {
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdusObj[i]);
                    smsReceiveTime = msgs[i].getTimestampMillis();
                    str += "SMS from " + msgs[i].getDisplayOriginatingAddress();
                    str += " :";
                    str += msgs[i].getDisplayMessageBody().toString();
                    str += "
";
                }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

...but when gujarati has been broken into more than one string

When a text message exceeds the maximum message length (which depends on the character set used), it can be broken up into parts, and sent as a multipart message. That's what's happening in your case, and the way you've structured your code, it appears like you're receiving several different texts.

When receiving a multipart message, you will end up with multiple SmsMessages in onReceive(). These are not individual messages, but the parts of a single message. The individual bodies must all be concatenated to assemble the one complete message.

The following is a simple example of how to retrieve received messages, for any number of parts, as well as the sender's number and the message's timestamp.

@Override
public void onReceive(Context context, Intent intent) {

    String number = "unknown";
    StringBuilder message = new StringBuilder();
    long timestamp = 0;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        SmsMessage[] msgs = Telephony.Sms.Intents.getMessagesFromIntent(intent);

        number = msgs[0].getDisplayOriginatingAddress();
        timestamp = msgs[0].getTimestampMillis();

        for (int i = 0; i < msgs.length; i++) {
            message.append(msgs[i].getDisplayMessageBody());
        }
    }
    else {
        Object[] pdus = (Object[]) intent.getSerializableExtra("pdus");
        for (int i = 0; i < pdus.length; i++) {
            byte[] pdu = (byte[]) pdus[i];
            SmsMessage msg = SmsMessage.createFromPdu(pdu);

            if (i == 0) {
                number = msg.getDisplayOriginatingAddress();
                timestamp = msg.getTimestampMillis();
            }

            message.append(msg.getDisplayMessageBody());
        }
    }

    String report = String.format("SMS from %s%nMessage : %s%nSent : %s",
                                  number,
                                  message.toString(),
                                  DateFormat.getDateTimeInstance()
                                            .format(new Date(timestamp)));

    ...
}

Starting with KitKat, the Telephony.Sms.Intents.getMessagesFromIntent() method is available to process the Intent extra that contains the message. It returns an array of SmsMessage that we simply need to loop over and pull the necessary data.

Before KitKat, that method was not publicly available, so we need to handle the processing ourselves. If you're not supporting versions prior to KitKat, then the example above can be pared down to remove the code specific to those earlier versions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...