android中如何安排稍后发送短信?

问题描述 投票:0回答:2

我正在尝试在 Android 应用程序中安排短信。短信正在发送,但不会被“安排”。这是我的代码。

Intent myIntent = new Intent(ScheduleMessage.this, MyAlarmService.class);

Bundle bundle = new Bundle();
bundle.putCharSequence("Number", Number.getText().toString());
bundle.putCharSequence("Message", Message.getText().toString());
myIntent.putExtras(bundle);

pendingIntent = PendingIntent.getService(ScheduleMessage.this, 0, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND,20);

alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

Toast.makeText(getApplicationContext(), "Message: "+Message+" for Number: "+Number, 1).show();

我是否遗漏了一些明显的东西?没有强制关闭或任何错误。只是,短信会立即发送,而不是在 30 秒后发送(我稍后会考虑按小时、天等进行安排)。请帮我。我对 Android 还很陌生。

android sms alarmmanager
2个回答
1
投票

这取决于执行代码时的当前确切时间。 :)

您要做的就是获取当前时间(例如 09:19:35),然后将

seconds
部分设置为 20(例如 09:19:20)。每分钟 40 秒,那是过去的时间,立即触发您的闹钟。

你想写的是:

calendar.add(Calendar.SECOND,20);

注意

add
方法,您在其中写了
set


0
投票

很棒的文章!在 iPhone 上安排短信可以让你保持井然有序。您的分步指南非常有用且用户友好。我很欣赏这些明确的说明,特别是对于我们这些可能不精通技术的人来说。有关安排短信的更深入的详细信息和其他提示,请查看我找到的这篇综合文章“如何在 iPhone 上安排短信”。它完美地补充了您的指南,并为最大限度地发挥该功能提供了宝贵的见解。感谢您简化了这个有用的 iPhone 功能!

© www.soinside.com 2019 - 2024. All rights reserved.