4

I want to be able to extract text from received SMS's. I'm not sure whether I should use content providers or the sms message is included in the intent received by broadcast receiver.

I have a broadcast receiver waiting for SMS's, and want to inspect the contents of the received message.

Thank you.

madu
  • 5,232
  • 14
  • 56
  • 96

2 Answers2

5

You can create SmsMessage instances from the Intent in your BroadcastReceiver as follows:

Bundle bundle = intent.getExtras();
Object[] pdus = (Object[])bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++) {
    messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
    //messages[i].getMessageBody();
}
Josef Pfleger
  • 74,165
  • 16
  • 97
  • 99
  • BTW Josef, why did you comment out getMessageBody()? I see in the API that I can use it and it seems much easier because it returns a String. – madu Nov 07 '10 at 02:35
  • I'm a little confused as to how many SmsMessages there will be in a single SMS. Because I want to use getOriginatingAddress(), and how I can call this function when I have more than one SmsMessage object. Thank you. – madu Nov 07 '10 at 03:03
  • The commented line was meant as a *hint* to how you can get to the message body :) Your `BroadcastReceiver` can receive more than one message at a time which is why it's an array. – Josef Pfleger Nov 07 '10 at 10:53
  • Thank you Josef. When you say can receive more than one message, do you mean one SMS with mulitiple parts (more than 160 letters), or multiple SMSs from multiple senders? If its the latter, why shouldn't broadcasts be received for each SMS individually? – madu Nov 07 '10 at 11:08
  • The array has more than one element when a multi-part SMS/MMS comes in. For multiple SMSes, from my experience with some testing with the emulator, the a new broadcast is fired for each one. – mbafford Mar 04 '11 at 01:52
2

Note that the length of each SMS in a multi-part (concatenated) SMS is not 160 chars, because each of them starts with a data header, so 160 chars is not the actual size of each SMS, it is the chars length from which the message becomes concatenated. Also, this boundary depends on the encoding of the message.

For more info see [link text][1] . This should be a comment and not an answer, but by the time of writing it my reputation does not allows me to leave comments.

[1]: http://en.wikipedia.org/wiki/SMS#Message_size message size

apps
  • 942
  • 5
  • 8