I got this code from somewhere, a book I think:
public class KITSMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// get the SMS message that was received
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
Log.d(LOGTAG, "DavyCrockettKingOfTheWildFrontier.onReceive");
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
String senderTel = "";
for (int i = 0; i < msgs.length; i++) {
// get the body of the message
msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
// get the msg body
str += msgs[i].getMessageBody().toString();
str += "\n";
}
. . .
…but now as I look at the comments I’m a bit bamboozled. One line says: “get the body of the message” and then the next line says, “get the msg body”. If this is really “the preferred method” for extracting the SMS body, why does it appear, according to the comments, to be done twice?
This is because
pdusis binary data which is converted into array ofSMSMessageobject usingwhile from this object to fetch actual message string below line is used