Android中的pdu

來源:互聯網
上載者:User

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();
}
}
 
作者:轉身淚傾城

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.