如题所述
问题:Android:SMSMessage类中用到的pdus这个变量是做什么用的?怎么解释它的含义?
回答:
Android设备接收到的SMS是以pdu形式的(protocol description unit)。所以从intent提取数据时就会遇到pdus。
publicclassSMSReceiverextendsBroadcastReceiver {
@Override
publicvoidonReceive(Contextcontext, Intent intent) {
//---接收传入的消息---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null; String str = "SMS from ";
if (bundle != null) {
//---查询到达的消息---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = newSmsMessage[pdus.length];
for (inti=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (i==0) {
//---获取发送者手机号---
str += msgs[i].getOriginatingAddress(); str += ":";
}
//---获取消息内容---
str += msgs[i].getMessageBody().toString();
}
//---显示SMS消息---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("SMSReceiver", str);
}
}
}
回答:
Android设备接收到的SMS是以pdu形式的(protocol description unit)。所以从intent提取数据时就会遇到pdus。
publicclassSMSReceiverextendsBroadcastReceiver {
@Override
publicvoidonReceive(Contextcontext, Intent intent) {
//---接收传入的消息---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null; String str = "SMS from ";
if (bundle != null) {
//---查询到达的消息---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = newSmsMessage[pdus.length];
for (inti=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
if (i==0) {
//---获取发送者手机号---
str += msgs[i].getOriginatingAddress(); str += ":";
}
//---获取消息内容---
str += msgs[i].getMessageBody().toString();
}
//---显示SMS消息---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Log.d("SMSReceiver", str);
}
}
}
温馨提示:答案为网友推荐,仅供参考