Non english SMS appears as multiple strings?

后端 未结 1 1542
北海茫月
北海茫月 2020-12-19 15:53

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 langua

相关标签:
1条回答
  • 2020-12-19 16:26

    ...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.

    0 讨论(0)
提交回复
热议问题