sending sms to multiple contacts using loop

2020-07-24 06:49发布

问题:

I am working on an android sms application.I have to send sms to multiple contact.There is method available to send sms to multi contacts so i used for loop.The following is my code.

for (int i = 0; i < to_nums.size();i++) {

                sendSms(to_nums.get(i),  snd_txt.getText().toString() );


        }

public void sendSms(final String phoneNumber, final String message){


    String SENT = "SMS_SENT";
    String DELIVERED = "SMS_DELIVERED";
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
        new Intent(SENT), 0);
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED),0);

   //--- When the SMS has been sent --

    sendBroadcastReceiver=new BroadcastReceiver() {


        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            switch (getResultCode()) {

                case Activity.RESULT_OK:

                    Toast.makeText(getBaseContext(), "SMS sent",
                        Toast.LENGTH_SHORT).show();

                    break;

                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(getBaseContext(), "Generic failure",
                        Toast.LENGTH_SHORT).show();

                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(getBaseContext(), "No service",
                        Toast.LENGTH_SHORT).show();



                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:

                    Toast.makeText(getBaseContext(), "Null PDU",
                        Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:

                    Toast.makeText(getBaseContext(), "Radio off",
                        Toast.LENGTH_SHORT).show();
                    break;



                default:
                    break;
            }
            context.unregisterReceiver(this);

        }
    };



    registerReceiver(sendBroadcastReceiver , new IntentFilter(SENT));






    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);





}

But when I am running my code I am getting an exception ,that says 'Receiver not registered'. for single receipient it works fone. .How can I register and unregister in order? Please help me friends.

回答1:

You can have a look at these :

Sending SMS to multiple contacts stored in a textview

and

How to send sms to multiple contacts and get the result code for each of them in android

and

Sending sms to multiple people in android



回答2:

Your problem is the part where you are creating new Intent Object with the same name for all numbers,when doing this you are overriding the registered broadcast with the newer number so when the older one catches a broadcast and runs the onReceive function it finds that its not registered any more! your solution is to change the Intent initialization to something like this:

Intent i = new Intent(SENT_INTENT + phoneNumber);

and the same for the delivery intent,let me know if your problem is solved