-1

I am developing an android app which gets the SMS using broadcast receiver when its comes. Here is the code:

if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) { Bundle bundle = intent.getExtras(); SmsMessage[] msgs; if (bundle != null) { //---retrieve the SMS message received--- try { Object[] pdus = (Object[]) bundle.get("pdus"); msgs = new SmsMessage[pdus.length]; for(int i=0; i<msgs.length; i++) { msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]); String address = msgs[i].getOriginatingAddress(); String body = msgs[i].getMessageBody(); } } catch(Exception e) { Log.d("Exception caught",e.getMessage()); } } }

Problem is that few messages I receive comes in two or three parts. I don't know how to join these parts of messages. How can I detect that the first part of message needed to be combined with the next part message.

First Part: Your airtel mobile ********** online recharge txn ID ************ o

Second Part: f Rs *** has been initiated. Please keep the txn id for future refe

Third Part: rence.

Mohammad Quadri
  • 375
  • 4
  • 12

2 Answers2

1

You need to add message body(parts). change this

          String body = msgs[i].getMessageBody();

to

          String body += msgs[i].getMessageBody();
Ajith Pandian
  • 1,332
  • 13
  • 21
0

The problem is inside your loop try this out, you should be fine

    Bundle bundle = intent.getExtras();

    SmsMessage[] msgs = null;

    String str = "";

    if (bundle != null) {
        // Retrieve the SMS Messages received
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];

        // For every SMS message received
        for (int i=0; i < msgs.length; i++) {
            // Convert Object array
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            // Sender's phone number
            str += "SMS from " + msgs[i].getOriginatingAddress() + " : ";
            // Fetch the text message
            str += msgs[i].getMessageBody().toString();              
            str += "\n";
        }

        // Display the entire SMS Message
        Log.d(TAG, str);
    }
Babajide Apata
  • 671
  • 6
  • 18