Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send Multi-part SMS with check for delivery when SMS recieved (caused receivercallnotallowedexception)

I provide below code in order to send Multi-part SMS with checking for Delivery in reply to sender :
ExecuteCommand is BroadCastReciver that receives incoming Sms and finally reply to sender one Sms with size >160 character and check sent and delivery status of all parts

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

and class SendSMS proivde BroadcastReceiver for send Intent to determine status of sending and delivery as :

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

} and finally sms receive correctly but send reply SMS casued receivercallnotallowedexception i think this caused by sentReceiver dynamic registerReceivering

like image 625
FxRi4 Avatar asked Jan 01 '26 00:01

FxRi4


1 Answers

My Guess is true ,the problem is that registering new Intent in another BroadcastReceiver (here ExecuteCommand) cause receivercallnotallowedexception I fix this problem as :

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  
                     }
like image 72
FxRi4 Avatar answered Jan 06 '26 10:01

FxRi4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!