Guimove
Guimove

Reputation: 17

How send messages to various phone number with ";" for separate (java android)?

    private void sendSMS(){

    String phoneNo = number.getText().toString().trim();
    String SMS = message.getText().toString().trim();

    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
        Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
    } catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();

    }
}

Hello, I have this piece of code that works successfully! But I want now sent message to various numbers with separate ";".

For example, in the emulator I want enter in the numbers zone (1254;2058;153348) and the massage has to be sent to all the contacts I have enter.

Thanks you for your answer!

Upvotes: 0

Views: 344

Answers (1)

snachmsm
snachmsm

Reputation: 19243

phoneNo.split(";") will give you String array with multiple contacts. use for loop for sending to multiple users/numbers

private void sendSMS(){
    String phoneNo = number.getText().toString().trim();
    String SMS = message.getText().toString().trim();

    if(phoneNo.contains(";"){
        String[] phoneNumbers = phoneNo.split(";");
        for(String number : phoneNumbers) sendSmsTo(SMS, number);
    }
    else sendSmsTo(SMS, phoneNo);
}

private void sendSmsTo(String SMS, String phoneNo){
    try {
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage(phoneNo, null,SMS,null,null);
        Toast.makeText(this,"SMS est envoyé",Toast.LENGTH_SHORT).show();
    } catch (Exception e){
        e.printStackTrace();
        Toast.makeText(this,"Erreur",Toast.LENGTH_SHORT).show();
    }
}

Upvotes: 1

Related Questions