广播接收器与sendMultiPartTextMessage

我一直在尝试从sendMultipartTextMessage得到状态结果代码,我不明白的是,如果广播接收器与sendTextMessage一起工作,为什么不与sendMultipartTextMessage一起工作? 我search了很多,看和代码示例,但只是不能看到任何明显的原因,为什么这将无法正常工作。 任何人都可以解释这一点吗?

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

谢谢。

Solutions Collecting From Web of "广播接收器与sendMultiPartTextMessage"

嗨,你错过了提供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(); } 

为下面发送的短信创build一个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; } } 

创build另一个广播接收机的短信交付如下。

 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> 

尝试将IntentFilter设置为(SENT)而不是(SENT_ACTION)。 它对我来说工作得很好。