I am learning how to send SMS in android, have seen the code as below:
public class SMSReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = “”;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get(“pdus”);
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += “SMS from “ + msgs[i].getOriginatingAddress();
str += “ :”;
str += msgs[i].getMessageBody().toString();
str += “\n”;
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
Now my question is how do I know what are the contents of Intent object that are passed into onReceive function? As below:
Object[] pdus = (Object[]) bundle.get(“pdus”);
How do I know there is a “pdus” key in the bundle object?
I can’t find any clue in the API doc, anyone know where is the related information located?
I don’t only want to know what the SMS intent pass into onReceive function, but also the other system related Intent, but I can’t locate any related information in the API doc. I wonder does the information really exist?
You can check if the bundle contains a Key value pair with key
pdususingbundle.containsKey("pdus")which returnstrueif there are anyProtocol Data Units(PDUs which in your case are SMSes).So, the check for processing the PDUs should be
if (bundle != null && bundle.containsKey("pdus"))