开发者

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 broadca开发者_开发百科st receiver.

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

Thank you.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜