class MessageReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i = 0 ; i < messages.length; i++) {
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
String address = messages[0].getOriginatingAddress();//获取发送发号码
String fullMessage = "";
for(SmsMessage message : messages) {
fullMessage += message.getMessageBody();//获取短信内容
}
sender.setText(address);
content.setText(fullMessage);
}
有几个问题不太明白:1、messages[0].getOriginatingAddress()是默认数组第一个数存放发送方号码吗?
2、foreach语句中的message对象是已经实例化了吗?
极客丶D