在Android上定期安排后台任务
我正在开发一个应用程序,我必须从多个传感器读取数据并每 15 分钟将其发送到远程服务器。这也必须在应用程序关闭/终止时完成,并且我还必须确保它始终执行。我还想确保它(几乎)恰好每 15 分钟发生一次(+-1 分钟差异是上限)。
此时,我找到了 3 个选项:使用 Workmanager、Alarmmanager 或使用前台服务。遵循文档,Workmanager 似乎是执行后台任务的方法,但是,在阅读了一些内容之后, Alarmmanager 似乎是一个更安全的选择(Workmanager 有时在打瞌睡模式下会出现问题,并且时间不准确,因为 它使用至少 5 分钟的弹性周期)。并且 前台服务 并不是真正允许执行此类任务的(实际上并不是这样)长时间运行,这只是一个周期性任务)并且在较新的 Android 版本中受到限制。您认为使用 Alarmmanger 来完成此任务是个好主意,还是应该使用其他东西?谢谢!
I'm working on an app where I have to read data from multiple sensors and send it to a remote server every 15 minutes. This has to be done when the app is closed/killed as well and I also have to be sure it always executes. I also want to be sure it happens (almost) exactly every 15 minutes (+-1 minute difference is the upper limit).
At this point, I've found 3 options: using Workmanager, Alarmmanager or using a foreground service. Following the documentation, Workmanager seems the way to go for background tasks, however, after having done some reading, Alarmmanager seems to be a safer choice (Workmanager sometimes has troubles with doze mode, and the timing isn't exact because it uses a flex period of at least 5 minutes). And a foreground service is not really allowed for this kind of task (it's not really long running, it's just a periodic task) and is being limited in newer Android versions. Do you think it would be a good idea to use an Alarmmanger for this task, or should I use something else? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TODO 后台调度.. 您可以使用此方法来做您的事情..
KOTLIN;
JAVA;
TODO Background scheduling.. You can use this method todo your stuff..
KOTLIN;
JAVA;