发送多部分短信并检查是否已接收短信(引起receivercallnotallowedexception)

3
我提供以下代码以发送多部分短信,并检查回复发送者的传递情况:
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);
    }

SendSMS提供了BroadcastReceiver,用于发送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);
}

最终短信接收正常,但发送回复短信引发了ReceiverCallNotAllowedException异常,我认为这是由于动态注册接收器时没有正确地注销导致的。

1个回答

2

我的猜测是正确的,问题在于在另一个广播接收器(这里是ExecuteCommand)中注册新的意图会导致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  
                     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接