2011-01-23 123 views
8

我一直在試圖從sendMultipartTextMessage得到狀態結果代碼,我不明白的東西是如果廣播接收器與sendTextMessage一起工作,爲什麼它不與sendMultipartTextMessage一起使用?我搜索範圍很廣,查找和代碼示例,但只是不能看到任何明顯的原因,爲什麼這將無法正常工作。任何人都可以對此有所瞭解嗎?廣播接收器與sendMultiPartTextMessage

ArrayList<String> messages = sms.divideMessage(text); 
int messageCount = messages.size(); 
ArrayList<PendingIntent> deliveryIntents = new ArrayList<PendingIntent>(messageCount); 
ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>(messageCount); 

for (int j = 0; j < messageCount; j++) { 
    sentIntents.add(
      PendingIntent.getBroadcast(context, 0, new Intent(SENT_ACTION), 
      0)); 
} 
sms.sendMultipartTextMessage(phoneNumber, null, messages, sentIntents, null); 

這裏是我的BroadcastReceiver

private BroadcastReceiver messageSentReceiver = new BroadcastReceiver() {  
    @Override 
    public void onReceive(Context context, Intent intent) { 
     switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 
}; 

的代碼是按以下步驟進行:

sentIntents = new ArrayList<PendingIntent>(); 
sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(SENT_ACTION), 0); 
deliveredIntent = PendingIntent.getBroadcast(context, 0, new Intent(DELIVERED_ACTION), 0); 

context.registerReceiver(messageSentReceiver, new IntentFilter(SENT_ACTION)); 
context.registerReceiver(messageDeliveredReceiver, new IntentFilter(DELIVERED_ACTION)); 

如果任何人都可以對這個有何啓示,我真的很感激它。我只是不明白爲什麼這可以與sendTextMessage,但不是sendMultipartTextMessage

謝謝。

回答

0

嘗試將IntentFilter設置爲(SENT)而不是(SENT_ACTION)。它對我來說工作得很好。

5

您好,您錯過了在您的意圖中提及brodcast接收器名稱。

請嘗試以下代碼,它的工作對我罰款..

ArrayList<PendingIntent> sentPendingIntents = new ArrayList<PendingIntent>(); 
    ArrayList<PendingIntent> deliveredPendingIntents = new ArrayList<PendingIntent>(); 
    PendingIntent sentPI = PendingIntent.getBroadcast(mContext, 0, 
      new Intent(mContext, SmsSentReceiver.class), 0); 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(mContext, 0, 
      new Intent(mContext, SmsDeliveredReceiver.class), 0); 
    try { 
     SmsManager sms = SmsManager.getDefault(); 
     ArrayList<String> mSMSMessage = sms.divideMessage(message); 
     for (int i = 0; i < mSMSMessage.size(); i++) { 
      sentPendingIntents.add(i, sentPI); 

      deliveredPendingIntents.add(i, deliveredPI); 
     } 
     sms.sendMultipartTextMessage(phoneNumber, null, mSMSMessage, 
       sentPendingIntents, deliveredPendingIntents); 

    } catch (Exception e) { 

     e.printStackTrace(); 
     Toast.makeText(mContext, "SMS sending failed...", 
       Toast.LENGTH_SHORT).show(); 
    } 

對發來的短信像下面創建一個boardcast接收器。

public class SmsSentReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
    switch (getResultCode()) { 
    case Activity.RESULT_OK: 
     Toast.makeText(context, 
       "SMS Sent" + intent.getIntExtra("object", 0), 
       Toast.LENGTH_SHORT).show(); 

     break; 
    case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
     Toast.makeText(context, "SMS generic failure", Toast.LENGTH_SHORT) 
       .show(); 

     break; 
    case SmsManager.RESULT_ERROR_NO_SERVICE: 
     Toast.makeText(context, "SMS no service", Toast.LENGTH_SHORT) 
       .show(); 

     break; 
    case SmsManager.RESULT_ERROR_NULL_PDU: 
     Toast.makeText(context, "SMS null PDU", Toast.LENGTH_SHORT).show(); 
     break; 
    case SmsManager.RESULT_ERROR_RADIO_OFF: 
     Toast.makeText(context, "SMS radio off", Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 

創建另一個廣播接收器的短信傳遞如下。

public class SmsDeliveredReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent arg1) { 
    switch (getResultCode()) { 
    case Activity.RESULT_OK: 
     Toast.makeText(context, "SMS delivered", Toast.LENGTH_SHORT).show(); 
     break; 
    case Activity.RESULT_CANCELED: 
     Toast.makeText(context, "SMS not delivered", Toast.LENGTH_SHORT).show(); 
     break; 
    } 
} 

}

最後,在清單申報接收器。

<receiver android:name=".receiver.SmsSentReceiver" > 
    </receiver> 
    <receiver android:name=".receiver.SmsDeliveredReceiver" > 
    </receiver> 
+0

是否需要創建兩個不同的類。 –