问题
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