sendMultiPartTextMessage

sendMultiPartTextMessage

本文介绍了广播接收机与sendMultiPartTextMessage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图从sendMultipartTextMessage获取状态的结果code,这件事我不明白的是,如果广播接收器的工作原理与sendTextMessage,为什么犯规它sendMultipartTextMessage工作?我搜索远近看去,code的例子,但都无法看到任何明显的原因,为什么这不会工作。任何人都可以阐明这个任何光线?

I've been trying to get the status result code from sendMultipartTextMessage, the thing i don't understand is if the broadcast receiver works with sendTextMessage, why doesnt it work with sendMultipartTextMessage? I've searched far and wide and looked and code examples but just cant see any obvious reasons as to why this wont work. Can anyone shed any light on this?


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

有关(INT J = 0; J&LT; messageCount; J ++){   sentIntents.add(PendingIntent.getBroadcast(上下文,0,                                              新的意图(SENT_ACTION)                                              0));   }sms.sendMultipartTextMessage(phoneNumber的,空,消息,sentIntents,NULL);

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


Here's my 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;
          }
      }
    };


在code完成如下:


The code is done as below:


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,新的IntentFilter(SENT_ACTION));context.registerReceiver(messageDeliveredReceiver,新的IntentFilter(DELIVERED_ACTION));

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

如果任何人都可以揭示这一点,我真的AP preciate任何光线。我只是不明白为什么这可能是工作与sendTextMessage但不是在sendMultipartTextMessage。

If anyone could shed any light on this, i would really appreciate it. I just can't see why this could work with sendTextMessage but not on sendMultipartTextMessage.

感谢。

推荐答案

嗨你缺少提brodcast接收器的名字在你的意图。

Hi you are missing to mention brodcast receiver name in your intent.

请$ C $下面尝试C时的正常工作对我来说..

Please try below code it's working fine for me..

    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接收器。

Create a boardcast receiver for sent sms like below.

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

创建另一个广播接收短信传递如下图所示。

Create another broadcast receiver for sms deliver as below.

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>

这篇关于广播接收机与sendMultiPartTextMessage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 02:14