ANDROID 延迟短信发送

发布于 2024-10-16 09:53:19 字数 1140 浏览 7 评论 0原文

我目前正在尝试编写一个应用程序来向数据库中的 300 多个号码发送大量短信。

我遇到了一次性发送所有邮件的问题,我的应用程序将强制关闭,而我只成功发送了 27/308。

我正在使用 for 循环发送短信。

这是否有解决办法,我可以在进入下一步之前将 for 循环延迟 1-2 秒?

目前我已经尝试过这段代码,但它只休眠 20 秒,然后它会一次性执行所有步骤,而不是每步 20 秒。注释掉我的sendSms方法并用println()进行测试;

任何帮助将不胜感激。

for (i = 0; i < phoneNumbers.length; i++){
                txtCommand = customIDs[i] + ";" + command + ";&W<";
                if (phoneNumbers[i].length()>0 && txtCommand.length()>0)  {
                    final String Messages = "Phone Number:" + phoneNumbers[i] + " " + "Message:" + txtCommand;
                    myHandler.postDelayed(new Runnable() { 
                        public void run() { 
                            System.out.println(Messages);
                            //sendSMS(phoneNumbers[i], txtCommand);    
                   } 
           }, 20000);
                    }
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }

I'm currently trying to code an app to send mass SMS to 300+ numbers I have in my database.

I'm facing issues with sending them all at one go my app will force close and I only managed to send like 27/308.

I'm using a for loop to send my SMSes.

Is this any fix for this where I can delay my for loop for like 1-2 seconds before going to the next step?

Currently I've tried this code but it only sleep for 20seconds then it'll do all the steps at one go instead of 20sec per step. Commented out my sendSms method and tested with println();

Any help would be greatly appreciated.

for (i = 0; i < phoneNumbers.length; i++){
                txtCommand = customIDs[i] + ";" + command + ";&W<";
                if (phoneNumbers[i].length()>0 && txtCommand.length()>0)  {
                    final String Messages = "Phone Number:" + phoneNumbers[i] + " " + "Message:" + txtCommand;
                    myHandler.postDelayed(new Runnable() { 
                        public void run() { 
                            System.out.println(Messages);
                            //sendSMS(phoneNumbers[i], txtCommand);    
                   } 
           }, 20000);
                    }
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }

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

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

发布评论

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

评论(1

婴鹅 2024-10-23 09:53:19

编写的代码会将所有内容排队,以便在 for 循环后 20 秒运行。但您想要的是将每个任务排队,在前一个任务运行 20 秒后运行。

您可以尝试将延迟乘以索引:

myHandler.postDelayed(
    ...
, (i + 1) * 20000);

或者,您可以递归地重写循环:

void queueMessage(final String[] phoneNumber, final int index) {
    if (index < phoneNumber.length) {
      // TODO do your validation here
      myHandler.postDelayed(new Runnable() {
          public void run() {
            // TODO do your work here
            queueMessage(phoneNumber, index + 1);
          }
        }, 20000);
    }
  }

顺便说一句,如果您尚未在服务中运行此循环,那么您应该这样做。

The code as written will queue everything to run 20 seconds after the for loop. But what you want is for each task to be queued to run 20 seconds after the previous one.

You could try multiplying your delay by the index:

myHandler.postDelayed(
    ...
, (i + 1) * 20000);

Or, you could rewrite your loop recursively:

void queueMessage(final String[] phoneNumber, final int index) {
    if (index < phoneNumber.length) {
      // TODO do your validation here
      myHandler.postDelayed(new Runnable() {
          public void run() {
            // TODO do your work here
            queueMessage(phoneNumber, index + 1);
          }
        }, 20000);
    }
  }

As an aside, if you're not already running this loop in a service, you should.

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