2011-01-23 10 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

(SENT_ACTION)ではなく、(SENT)にIntentFilterを設定してみてください。それは私のためにうまくいきます。

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(); 
    } 

は、以下のように送信されたSMSのための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; 
    } 
} 

sms配信用の別のブロードキャスト受信者を以下のように作成します。

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

2つの異なるクラスを作成する必要がありますか? –

関連する問題