【问题标题】:Send Multi-part SMS with check for delivery when SMS recieved (caused receivercallnotallowedexception)收到短信时发送多部分短信并检查是否送达(导致接收呼叫不允许异常)
【发布时间】:2014-01-04 13:14:15
【问题描述】:

我提供以下代码,以便发送多部分短信并检查发送以回复发件人:
ExecuteCommandBroadCastReciver,它接收传入的短信,最后回复发件人一条带有 size >160 字符的短信并检查所有零件的发送和交付状态

   public class ExecuteCommand extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            ... 
            SendSMS s=new SendSMS(context);
            s.SendMultipartSMS(MsgBody,Number);
    }

和 class SendSMS proivde BroadcastReceiver for send Intent 确定发送和交付状态为:

public class SendSMS {
    Context smscontext;
    private SmsManager smsManager;
    private BroadcastReceiver sentReceiver;
    int msgParts;

    public SendSMS(Context context) {
    smscontext = context;
}

public void SendMultipartSMS(String smsResult, String OriginatingAddress) {
    smsManager = SmsManager.getDefault();
    ArrayList<String> parts = smsManager.divideMessage(smsResult);
    msgParts = parts.size();
    final String SendNumber=OriginatingAddress;
    sentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            boolean anyError = false;
            switch (getResultCode()) {
            case Activity.RESULT_OK:
                break;
            case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
            case SmsManager.RESULT_ERROR_NO_SERVICE:
            case SmsManager.RESULT_ERROR_NULL_PDU:
            case SmsManager.RESULT_ERROR_RADIO_OFF:
                anyError = true;
                break;
            }
            msgParts--;
            if (msgParts == 0) {
                context.unregisterReceiver(sentReceiver);
                Log.d("smsresiver", "send ok");

                if (anyError) {
                    Toast.makeText(context, "sending sms fail",
                            Toast.LENGTH_SHORT).show();
                    } 
            }
        }
    };
    smscontext.registerReceiver(sentReceiver, new IntentFilter(
            "CTS_SMS_SEND_ACTION"));
       Intent mSendIntent = new Intent("CTS_SMS_SEND_ACTION");


    ArrayList<PendingIntent> sentIntents = new ArrayList<PendingIntent>();


    for (int i = 0; i < parts.size(); i++) {
        sentIntents.add(PendingIntent.getBroadcast(smscontext, 0, mSendIntent,
                0));
    }
    smsManager.sendMultipartTextMessage(
            OriginatingAddress, null, parts,
            sentIntents, null);
}

} 最后短信接收正确但发送回复短信 casued receivercallnotallowedexception 我认为这是由 sentReceiver 动态 registerReceivering 引起的

【问题讨论】:

    标签: android sms broadcastreceiver


    【解决方案1】:

    我的猜测是正确的,问题是在另一个 BroadcastReceiver(这里是 ExecuteCommand)中注册新 Intent 会导致 receivercallnotallowedexception 我将这个问题修复为:

    public class ExecuteCommand extends BroadcastReceiver {
           public void onReceive(Context context, Intent intent) {
                    // TODO Auto-generated method stub
                    if (intent.getAction()
                        .equals("android.provider.Telephony.SMS_RECEIVED")) {
                            //provide sending SMS in reply to send
                         }
                         else if (intent.getAction().equals("CTS_SMS_SEND_ACTION")) {
                            //check send and delivery of each SMS part  
                         }
    

    【讨论】:

      猜你喜欢
      • 2012-08-05
      • 2012-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-23
      相关资源
      最近更新 更多