Android:以同步方式发送短信

发布于 2024-12-20 20:49:18 字数 613 浏览 0 评论 0原文

我指的是这篇文章,用于从 Android 发送短信。

我尝试通过实施循环向 10 个不同的号码发送 10 条短信。 例如循环 sendSMS(phoneNo, message); 但发送 6 条短信后,sms.sendTextMessage(phoneNumber, null, message, sentPI, DeliveredPI); 给出 NullPointerExeption。

仅供参考,我在 DeliveredPI 中传递 null,因为我不关心短信的发送。但了解短信是否真的发送非常重要,这可以通过注册广播接收器来检索。

我已经注册了一个广播接收器,但似乎整个过程没有同步。只有在收到前一条短信的状态后,我们才应该发送第二条短信。

我假设减慢发送短信的速度将帮助我删除 NullPointerException。等待状态将是保持发送另一条短信之间的时间间隔的最佳主意。

简而言之,我想做->发送一条短信 ->等待状态->更新数据库中的状态 ->再发送一条短信。

I am referring this article for sending an sms from Android.

I tried sending 10 sms to 10 different numbers with implementing loop.
e.g. looping over sendSMS(phoneNo, message);
But after sending 6 sms, sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI); gives NullPointerExeption.

FYI, I am passing null in deliveredPI as I don't care about the delivery of sms. But it is very important to know if sms is really sent, and this can be retrieved by registering broadcast receiver.

I've registered a broadcast receiver, but it seems like whole process has no synchronization. We should send second sms only after we receive status of previous sms.

I am assuming that slowing down the speed of sending an sms will help me to remove NullPointerExeption. And waiting for status will be the best idea to keep time gap between sending another sms.

In short, I'd like to do -> send one sms -> wait for status -> update status in db -> send another sms.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤独患者 2024-12-27 20:49:19

我个人还没有测试我发布的代码,但它被接受作为 这个其他问题所以我认为它会起作用。

    SmsManager smsMan = new SmsManager.getDefault();
    ArrayList<String> contactList = new ArrayList();
    //add contacts to contactList with contactList.add(string)
    for (int i = 0; i <= contactList().size(); i++) {
    String SENT = contactList.get(i).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

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

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                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;
                }
            }
        }, new IntentFilter(SENT));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}

通过上述方式,您将向 Android 发送一个请求,依次发送每条消息。如果您想在 Activity.RESULT_OK 之后实际发送下一条短信,那么我建议您仍然使用 ArrayList 方法,但您可以使用类似的方法来代替 for 循环:

public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
    //add contacts to contactList with contactList.add(string)
    String SENT = contactList.get(position).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

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

            //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK
                        context.unregisterReceiver(this);
                        i++;
                        if (contactList.size()<i){
                            sendSms(i);
                        } else {
                            //You are done sending - Do what you want.
                        }
                        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;
                    }
                }
            }, new IntentFilter(SENT));
    smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);

}

再次。我还没有测试过,但它应该可以工作。如果您还有其他问题或者我有任何不清楚的地方,请告诉我。

I personally haven't tested the code I'm posting, but it was accepted as the answer for this other question so I assume it will work.

    SmsManager smsMan = new SmsManager.getDefault();
    ArrayList<String> contactList = new ArrayList();
    //add contacts to contactList with contactList.add(string)
    for (int i = 0; i <= contactList().size(); i++) {
    String SENT = contactList.get(i).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

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

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                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;
                }
            }
        }, new IntentFilter(SENT));
smsManager.sendTextMessage(contactList.get(i).toString(), null, message, sentPI, null);
}

The above way, you will send a request to Android to send each message one after another. If you want to actually send the next SMS after Activity.RESULT_OK, then I would recommend still using the ArrayList approach, but instead of a for loop, you could have something like:

public void onCreate(Bundle savedInstanceState) {
smsMan = new SmsManager.getDefault(); //assuming you declared SmsManager smsMan in class body.
contactList = new ArrayList(); //assuming you declared ArrayList<String> contactList in class body.
//add contacts to contactList with contactList.add(string);
}
public void sendSms(int position){
    //add contacts to contactList with contactList.add(string)
    String SENT = contactList.get(position).toString();// you could replace this with i,
    //or something like "sms_sent_myappname" + i.toString());

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

            //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK
                        context.unregisterReceiver(this);
                        i++;
                        if (contactList.size()<i){
                            sendSms(i);
                        } else {
                            //You are done sending - Do what you want.
                        }
                        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;
                    }
                }
            }, new IntentFilter(SENT));
    smsManager.sendTextMessage(contactList.get(position).toString(), null, message, sentPI, null);

}

Again. I haven't tested it, but it should work. Let me know if you have any more questions or if I was unclear about anything.

星光不落少年眉 2024-12-27 20:49:19

我将我的评论作为答案,因为我实际上无法发表评论。
Jakar,您提供的解决方案将不起作用。您不会在任何地方注销广播接收器。您的代码将引发错误。

I'm putting my comment as an answer because I can't actually comment.
Jakar, your solution you provided will NOT work. You are not unregistering the broadcast receivers anywhere. Your code will raise an error.

〆凄凉。 2024-12-27 20:49:19

我想你已经找到答案了。但由于问题仍然存在......它会使用 sendMultipartTextMessage() 吗?使用 sendTextMessage() 时有 160 个符号的限制,这是标准的单条 SMS 消息最大长度。

I assume you found your answer already. But since the question still remains here... would it be using sendMultipartTextMessage()? There is a limit equal 160 signs while using sendTextMessage(), which is a standard single SMS message maximum length.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文