如何获得在Android SDK中的pdu格式的短信SMS

问题描述:

有人可以让我知道,如果有一种方法来获得收件箱短信在Pdu格式?如何获得在Android SDK中的pdu格式的短信SMS

我认为这将有助于

 Object[] pdus = (Object[]) extras.get("pdus"); 
    for (Object pdu : pdus) { 
    SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdu); 
} 
+0

嗨DjHacktor ...感谢乌拉圭回合的快速回复。我有一些混淆,上面的代码是否会以pdu格式获得收件箱中的短信(已经存在)。 –

+0

不,它不会得到短信,你需要使用光标抓取从收件箱中获取短信,或者如果你想接收传入的短信,那么这是广播接收机的工作 – DjHacktorReborn

SmsMessage[] msgs = null; 
Object[] pdus = (Object[]) mBundle.get("pdus"); 
      if(pdus != null){ 
       msgs = new SmsMessage[pdus.length]; 
       Log.e(TAG, "pdus length : "+pdus.length); 
       for(int k=0; k<msgs.length; k++){ 
        msgs[k] = SmsMessage.createFromPdu((byte[])pdus[k]); 
        Log.e(TAG, "getDisplayMessageBody : "+msgs[k].getDisplayMessageBody()); 
        Log.e(TAG, "getDisplayOriginatingAddress : "+msgs[k].getDisplayOriginatingAddress()); 
        Log.e(TAG, "getMessageBody : "+msgs[k].getMessageBody()); 
        Log.e(TAG, "getOriginatingAddress : "+msgs[k].getOriginatingAddress()); 
        Log.e(TAG, "getProtocolIdentifier : "+msgs[k].getProtocolIdentifier()); 
        Log.e(TAG, "getStatus : "+msgs[k].getStatus()); 
        Log.e(TAG, "getStatusOnIcc : "+msgs[k].getStatusOnIcc()); 
        Log.e(TAG, "getStatusOnSim : "+msgs[k].getStatusOnSim()); 
        String dateTime=getDate(msgs[k].getTimestampMillis()); 
         messageTypeArray = messageTypeArray + "Inbox" + splitKey; 
         messageDateArray = messageDateArray + dateTime.substring(0, 11) + splitKey; 
         messageTimeArray = messageTimeArray + dateTime.substring(11, dateTime.length()) + splitKey; 
         messageNumberArray = messageNumberArray + msgs[k].getDisplayOriginatingAddress() + splitKey; 
         messageInformationArray = messageInformationArray + msgs[k].getDisplayMessageBody() + splitKey; 

IF you want all inbox message 

Uri uriSMSURI = Uri.parse("content://sms"); 
      Cursor cur = MainActivity.context.getContentResolver().query(uriSMSURI, null, null, null,null); 
      String sms = ""; 

      String type = ""; 

      while (cur.moveToNext()) 

      { 

       int t = cur.getInt(9); 
       if(t == 1) 
       { 
        type = "Inbox"; 
       } 
       else if(t == 2) 
       { 
        type = "Send"; 
       } 
       else if(t == 3) 
       { 
        type = "Draft"; 
       } 

       Date callDayTime = new Date(Long.valueOf(cur.getLong(4)));    
       SimpleDateFormat timeFormat = new SimpleDateFormat("h:m a"); 
       String time = timeFormat.format(callDayTime);   
       SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy:MM:d"); 
       String date = dateFormat.format(callDayTime); 

       messageTypeArray = messageTypeArray + type + splitKey; 
       messageDateArray = messageDateArray + date + splitKey; 
       messageTimeArray = messageTimeArray + time + splitKey; 
       messageNumberArray = messageNumberArray + cur.getString(2) + splitKey; 
       messageInformationArray = messageInformationArray + cur.getString(12) + splitKey; 

       sms += "From :" + cur.getString(2) + "\nMessage : " + cur.getString(12)+"\nType : " + type + "\nDate : " + date + "\nTime : " + time + "\n---------------\n";   
      // text.setText(sms); 


      } 
      cur.close();