0

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

navid abutorab
  • 189
  • 1
  • 2
  • 9
  • Your Receiver is only ever going to be delivered one message at a time, no matter how many parts it has. You need to concatenate the message bodies in the `for` loop to get the single, complete message. – Mike M. Sep 10 '16 at 07:16

1 Answers1

1

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

Mahdi Astanei
  • 1,905
  • 1
  • 17
  • 30