发送短信给多人在安卓 [英] Sending sms to multiple people in android

查看:195
本文介绍了发送短信给多人在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有无论如何,我可以发送短信到多个号码的使用SmsManager的人。我知道,我可以通过接触运行一个循环,并分别发送短信,但我想,有可能是一个办法做到这一点。

I want to know if there is anyway that I can send sms to multiple number of people using the SmsManager. I know that I can run a loop through the contacts and send sms individually but I figured that there may be a way to do this.

在code我用的是如下:

The code I use is given below:

SmsManager.getDefault().sendTextMessage("PHONE_NOS", null,msg.getText().toString(), sentPI, deliveredPI);

PS:我曾试过用;作为隔板。但这种情况的唯一事情是,它发送一个短信仅在列表中的第一人

PS: I have tried using ";" as a separator. But the only thing that happens is that it sends an sms only to the first person in the list.

有关的人谁看到这么晚的好处,它不可能通过发送短信给多人。正如比尔·莫特所指出的那样,如果有这样的事情有可能,就不会有这将采取名单,即用数字一个作为参数的API。 因此,唯一的可能的解决方案是有一个迭代器的数量和它们发送一次一个。

For the benefit of people who are seeing this late, its not possible to send SMS to multiple people. As Bill Mote has pointed out, if there was such a thing possible, there would have been an API which would have taken a "List-of-numbers" as an argument. So the only possible solution is to have an iterator for the numbers and send them one at a time.

推荐答案

净净净这里不能没有通过循环迭代和发送1消息1收件人完成。

The net-net-net here is it cannot be done without iterating through a loop and sending 1 message to 1 addressee.

我花了1/2的星期六试图做到这一点非常的事。我不能使它与工作;,,或\ N。我应该尝试硬编码2收信人所有的分隔符先分开了,但我也了解了Android SDK的一个宝贵的教训:如果他们想你发送超过1收件人在一个时间,然后他们会接受一个ArrayList或字符串数组,而不是一个单一的字符串;)

I spent 1/2 a Saturday trying to do this very thing. I could not make it work with ";", ",", " ", or "\n". I should have tried hard-coding 2 addressees separated by all the delimiters first, but I did learn a valuable lesson about the Android SDK: if they wanted you to send to more than 1 addressee at a time then they'd accept an ArrayList or an array of Strings rather than a singular String ;)

protected void sendMsg(Context context, SmsMessage smsMessage) {
        SmsManager smsMgr = SmsManager.getDefault();
        ArrayList<string> smsMessageText = smsMgr.divideMessage(smsMessage.getMsgBody());
        PendingIntent sentPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_SENT"), 0);
        PendingIntent deliveredPI = PendingIntent.getBroadcast(context, 0, new Intent("SMS_DELIVERED"), 0);
        int AddresseesPerMessage = 10;
        StringBuilder builder = new StringBuilder();
        String delim = "";
        for (ContactItem c:smsMessage.getAddresseeList()) {
            //  For every phone number in our list
            builder.append(delim).append(c.getPhoneNumber().toString());
            delim=";";
            if (((smsMessage.getAddresseeList().indexOf(c)+1) % AddresseesPerMessage) == 0 || smsMessage.getAddresseeList().indexOf(c)+1 == smsMessage.getAddresseeList().size()) {
                // using +1 because index 0 mod 9 == 0 
                for(String text : smsMessageText){
                    //  Send 160 bytes of the total message until all parts are sent
                    smsMgr.sendTextMessage(builder.toString(), null, text, sentPI, deliveredPI);
                }
                builder.setLength(0);
                delim="";
            }
        }
    }

这篇关于发送短信给多人在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆