Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

06 Jan 2011

Get entire message body from an Intent

I recently started programming the Android and noticed that most examples for processing an incoming SMS are not entirely correct.

An SMS message is limited to 160 characters. Current mobile phones break up a larger message in multiple messages transparently for the user. When Android notifies you about an incoming SMS it has all parts (of that large message) available. So here is how you reconstruct the entire message body from an Intent

Bundle bundle = intent.getExtras();
if (bundle == null) return;

StringBuilder message = new StringBuilder();  
Object[] pdus = (Object[]) bundle.get("pdus");

// Rebuild this entire message from the multi part smses/pdus  
for (Object pdu : pdus){
  // Notice that i use the deprecated android.telephony.gsm.SmsMessage
  // android.telephony.SmsMessage throws when i call createFromPdu
  SmsMessage msg = SmsMessage.createFromPdu((byte[])pdu);
  message.append(msg.getMessageBody().toString());  
}