madu
madu

Reputation: 5450

Android - Extract text from SMS

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.

Upvotes: 4

Views: 4266

Answers (2)

apps
apps

Reputation: 942

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

Upvotes: 2

Josef Pfleger
Josef Pfleger

Reputation: 74527

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();
}

Upvotes: 5

Related Questions