2012-03-22 28 views
7

tài liệu Android cho SMSManagers sendTextMessage chức năngCách "deliveryIntent" hoạt động trong khung SMS của Android?

public void sendTextMessage (String destinationAddress, String scAddress, String text,   
PendingIntent sentIntent, PendingIntent deliveryIntent) 

deliveryIntentnếu không NULL PendingIntent này là phát sóng khi tin nhắn được gửi đến người nhận. Pdu thô của báo cáo trạng thái nằm trong dữ liệu mở rộng ("pdu")

Tôi không thể hiểu được nếu deliveryIntent được kích hoạt khi SMS được gửi đến đíchAddress hoặc scAddress và ý nghĩa của "nguyên pdu của trạng thái báo cáo nằm trong dữ liệu mở rộng ("pdu") "và cách nhận báo cáo đó? .

Tôi đánh giá cao nỗ lực của bạn.

Trả lời

3

Phát sóng khi tin nhắn được gửi đến destinationAddress.

PDU có thể được trích xuất từ ​​Intent.getExtras().get("pdu") khi đã đăng ký BroadcastReceiver nhận được Intent Broadcast mà bạn xác định với PendingIntent.getBroadcast(Context, int requestCode, Intent, int flags). Ví dụ:

private void sendSMS(String phoneNumber, String message) {  
    String DELIVERED = "DELIVERED"; 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
     new Intent(DELIVERED), 0); 

    registerReceiver(
     new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context arg0, Intent arg1) { 
       Object pdu = arg1.getExtras().get("pdu"); 
       ... // Do something with pdu 
      } 

     }, 
     new IntentFilter(DELIVERED));   

    SmsManager smsMngr = SmsManager.getDefault(); 
    smsMngr.sendTextMessage(phoneNumber, null, message, null, deliveredPI);    
} 

Sau đó, bạn cần phải phân tích chiết xuất PDU, SMSLib sẽ có thể làm điều đó.

2

Chỉ cần để xây dựng dựa trên câu trả lời của a.ch, heres làm thế nào bạn có thể trích xuất các báo cáo giao hàng từ một ý định:

public static final SmsMessage[] getMessagesFromIntent(Intent intent) { 
    Object[] messages = (Object[]) intent.getSerializableExtra("pdus"); 
    if (messages == null || messages.length == 0) { 
     return null; 
    } 

    byte[][] pduObjs = new byte[messages.length][]; 

    for (int i = 0, len = messages.length; i < len; i++) { 
     pduObjs[i] = (byte[]) messages[i]; 
    } 

    byte[][] pdus = new byte[pduObjs.length][]; 
    SmsMessage[] msgs = new SmsMessage[pdus.length]; 
    for (int i = 0, count = pdus.length; i < count; i++) { 
     pdus[i] = pduObjs[i]; 
     msgs[i] = SmsMessage.createFromPdu(pdus[i]); 
    } 

    return msgs; 
} 

tín dụng đầy đủ cho dự án lớn tại địa chỉ: http://code.google.com/p/android-smspopup/

Các vấn đề liên quan