androidandroid-broadcastandroid-sms

android - How to combine two sms with smslistener


I've a sms listener that read sms from a certain number . It reads the sms without any problem but there is a bug here , if the message body is very big and it goes into 2 messages, the listener only detect the first one and cannot doesn't understand it should read two messages.

this is mycode :

if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
            Bundle bundle = intent.getExtras();
            settings = context.getSharedPreferences("settings", context.MODE_PRIVATE);
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null) {
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];
                if (msgs != null) {
                    for (int i = 0; i < msgs.length; i++) {
                        msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();

}}}

How can I solve this


Solution

  • try this

    Bundle bundle = intent.getExtras();
    messages = (Object[]) bundle.get("pdus");
    smsMessage = new SmsMessage[messages.length];
    for (int n = 0; n < messages.length; n++) {
        smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
    }
    
    for (int i = 0; i < smsMessage.length; i++)
        mainsms += smsMessage[i].getMessageBody();
    

    And your complete SMS text is mainsms