Android - Extract text from SMS

試著忘記壹切 提交于 2019-12-07 10:58:52

问题


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.


回答1:


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



回答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



来源:https://stackoverflow.com/questions/4113829/android-extract-text-from-sms

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!