Send one SMS to Multiple phone numbers

i have an arraylist of phone numbers. when i send a message it only sends to the last number on the list. here is my code

public void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CONTACT_PICK_REQUEST && resultCode == RESULT_OK){

        ArrayList<Contact> selectedContacts = data.getParcelableArrayListExtra("SelectedContacts");

        String display="";
        for(int i=0;i<selectedContacts.size();i++){

            display += (i+1)+"   "+selectedContacts.get(i).toString()+"n";
            theNumber=selectedContacts.get(i).phone;

        }
        contactsDisplay.setText("Selected Contacts : nn"+display);

    }

}


protected void sendMsg(String theNumber, String myMsg) {
    String SENT = "Message Sent";
    String DELIVERED = "Message Delivered";

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

    registerReceiver(new BroadcastReceiver() {
        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:

                        Toast.makeText(MainActivity.this, "SMS SENT", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(MainActivity.this, "Generic Failure", Toast.LENGTH_LONG).show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(MainActivity.this, "No Service", Toast.LENGTH_LONG).show();
                    break;
            }
        }
    }, new IntentFilter(SENT));

    registerReceiver(new BroadcastReceiver() {

        public void onReceive(Context arg0, Intent arg1) {
            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(getBaseContext(), "SMS delivered", Toast.LENGTH_LONG).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(getBaseContext(), "SMS not delivered", Toast.LENGTH_LONG).show();
                    break;
            }

        }
    }, new IntentFilter(DELIVERED));

    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(theNumber, null, myMsg, sentPI, deliveredPI);

}

}


Your loop is simply replacing the numbers from the list to theNumber variable.

theNumber=selectedContacts.get(i).phone;

When the loop ends, it finds the variable theNumber updated with last number from the list.

链接地址: http://www.djcxy.com/p/23932.html

上一篇: 图像裁剪在Android 7.0及更高版本中不起作用

下一篇: 发送一条短信至多个电话号码