ANDROID 延迟短信发送
我目前正在尝试编写一个应用程序来向数据库中的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写的代码会将所有内容排队,以便在 for 循环后 20 秒运行。但您想要的是将每个任务排队,在前一个任务运行 20 秒后运行。
您可以尝试将延迟乘以索引:
或者,您可以递归地重写循环:
顺便说一句,如果您尚未在服务中运行此循环,那么您应该这样做。
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:
Or, you could rewrite your loop recursively:
As an aside, if you're not already running this loop in a service, you should.