当前位置:操作系统 > 安卓/Android >>

Android中的pdu

Android设备接收到的SMS是以pdu形式的(protocol description unit)。android.telephony.gsm.SmsMessage这个类可以储存SMS的相关信息,我们也可以从接收到的pdu中创建新的SmsMessage实例,Toast界面组件可以以系统通知的形式来显示接收到的SMS消息文本。

A PDU is a "protocol discription unit", which is the industry format for an SMS message. because SMSMessage reads/writes them you shouldn't need to disect them. A large message might be broken into many, which is why it is an array of objects.


public class SMSReceiver extends BroadcastReceiver
{
/*当收到短信时,就会触发此方法*/
public void onReceive(Context context, Intent intent)
{
  Bundle bundle = intent.getExtras();
  Object messages[] = (Object[]) bundle.get("pdus");
  SmsMessage smsMessage[] = new SmsMessage[messages.length];
  for (int n = 0; n < messages.length; n++)
  {
   smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
  }
  //产生一个Toast
  Toast toast = Toast.makeText(context, "短信内容: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
  //设置toast显示的位置
  //toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 200);
  //显示该Toast
  toast.show();
}
}
 
作者:转身泪倾城

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,