Search code examples
androidsmsinbox

Android Inbox SMS receiving in different parts


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.


Solution

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

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

    to

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