如何在 Android 中设置持久/定期计划?
如何在每个指定时间(例如每天凌晨 5 点)执行一个操作(可能是一个 Intent)?它必须在设备重新启动后保留,类似于 cron 的工作原理。
我不确定是否可以使用 AlarmManager
来实现此目的,或者可以吗?
How can I execute an action (maybe an Intent) on every specified time (e.g. Every day on 5AM)? It has to stay after device reboots, similar to how cron works.
I am not sure if I can use AlarmManager
for this, or can I?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您希望它在设备重新启动后保留,则必须在设备重新启动后安排警报。
您需要在 AndroidManifest.xml 中拥有 RECEIVE_BOOT_COMPLETED 权限
还需要 BroadcastReceiver 来捕获意图 ACTION_BOOT_COMPLETED
最后,重写 BroadcastReceiver 中的 onReceive 方法。
编辑:查看 setRepeating 方法来安排“Android cron”。
If you want it to stay after the device reboots, you have to schedule the alarm after the device reboots.
You will need to have the RECEIVE_BOOT_COMPLETED permission in your AndroidManifest.xml
A BroadcastReceiver is needed as well to capture the intent ACTION_BOOT_COMPLETED
Lastly, override the onReceive method in your BroadcastReceiver.
Edit: Look at the setRepeating method of AlarmManager to schedule the 'Android cron'.
使用 BuzzBox SDK,您可以在应用程序中安排 cron 作业,执行以下操作:
其中“0 8-19 * * 1,2,3,4,5”是一个 cron 字符串,它将每小时运行一次任务,从上午 8 点到晚上 7 点,周一至周五。
你的任务可以是任何你想要的,你只需要实现一个 doWork 方法。该库将负责重新安排重新启动、获取唤醒锁以及重试错误。
有关 BuzzBox SDK 的更多信息请点击此处...
Using the BuzzBox SDK you can schedule a cron job in your App doing:
Where "0 8-19 * * 1,2,3,4,5" is a cron string that will run your Task once an hour, from 8am to 7pm, mon to fri.
You Task can be whatever you want, you just need to implement a doWork method. The library will take care of rescheduling on reboot, of acquiring the wake lock and on retrying on errors.
More info about the BuzzBox SDK here...