android - 收到 SMS 时发送多部分 SMS 并检查是否送达(导致 receive call notallowedexception)

标签 android sms broadcastreceiver

我提供以下代码以发送多部分短信并检查是否已送达以回复发件人:
ExecuteCommand 是接收传入短信的 BroadCastReciver 和最后回复发件人一条 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 proivde 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 我认为这是由 sentReceiver 动态 registerReceivering 引起的

最佳答案

我的猜测是正确的,问题是在另一个 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  
                     }

关于android - 收到 SMS 时发送多部分 SMS 并检查是否送达(导致 receive call notallowedexception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20892405/

相关文章:

android - 如果手机( native )中存在多条未读短信,如何通知从 native (手机)读取了哪些短信?

android - 更新 : as3 air for Android get device phone number

android - 如何以编程方式启动android手机?

java - 在 Android 中静态注册自定义广播接收器的正确方法是什么?

android - 如何将线性布局与垂直中心对齐?

java - 使用非递归回溯算法生成迷宫的问题

broadcastreceiver - 应用安装后删除 Apk

Android:我的警报管理器广播接收器不工作

android - 在 android 中本地化日期

python - 我如何着手编写一个程序来使用 python 发送和接收短信?