Bora
Bora

Reputation: 1925

Sending Message to Multiple Numbers using Intent

Hi i have a list of numbers.

ArrayList<String> numbers;

I want to sent message to all these number using Intent, together . I did this with email like sending email to multiple people , How to do for message ?

public static void send(Context ctx, String[] addy, String subject,
        String body,File attachment) {
    try {
        Intent sendIntent = new Intent(Intent.ACTION_VIEW);
        sendIntent.setType("vnd.android-dir/mms-sms");
        sendIntent.putExtra(android.content.Intent.EXTRA_PHONE_NUMBER,
                addy);
        sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
        sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
        //sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
        ctx.startActivity(Intent.createChooser(sendIntent,
                "Send via which Application?"));
    } catch (Exception e) {
        Toast.makeText(ctx, "No activity was found to handle this action",
                Toast.LENGTH_SHORT).show();
    }
}

This is opening the message app but the numbers stored in addy are not listed in message sent to column

Upvotes: 0

Views: 1617

Answers (2)

ilatyphi95
ilatyphi95

Reputation: 605

for java developer use

public static void send(Context ctx, String[] address, String subject,
                        String body, File attachment) {

    List<String> mylist = Arrays.asList(address);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);

    StringJoiner sj = new StringJoiner(";", "smsto:", "");
    mylist.forEach(sj::add);
    sendIntent.setData(Uri.parse(sj.toString()));

    sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

    if(sendIntent.resolveActivity(ctx.getPackageManager()) != null) {
        ctx.startActivity(sendIntent);
    }
}

for those using kotlin, use

fun send(ctx: Context, address: Array<String>, subject: String, body: String, attachment: File) {

    val sendIntent = Intent(Intent.ACTION_VIEW)

    val data = address.toList().joinToString(";", "smsto:")

    sendIntent.data = Uri.parse(data)
    sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject)
    sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, body)

    if(sendIntent.resolveActivity(ctx.packageManager) != null) {
        ctx.startActivity(sendIntent)
    }
}

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006604

I did this with email like sending email to multiple people

Assuming that you used a third-party application via an Intent action, whether this works is up to the author of the third-party application. You have no way of knowing whether each of the hundreds of email apps for Android support specifying multiple addresses, unless you test them all.

How to do for message ?

You send them one at a time.

Your code is dreadful, using an undocumented MIME type that will not necessarily be honored on all devices, but EXTRA_PHONE_NUMBER is supposed to be a String, not a String[].

ACTION_SEND, or ACTION_SENDTO with an smsto: address, similarly is only guaranteed to support a single number and will give you the same problems as you have with sending email that way.

SmsManager has methods for directly sending SMS; they too only support one phone number at a time.

Upvotes: 1

Related Questions