Android建议可靠的API进行定期背景工作?

发布于 2025-01-29 01:50:49 字数 255 浏览 4 评论 0 原文

我一直在使用Workmanager为我的应用程序创建通知。出于我的目的,我认为定期工作是最合适的,但是经过一些测试和在线阅读之后,它似乎非常不可靠。使用最小间隔(15分钟),然后关闭应用程序,工人醒来了5-6次,然后似乎被杀死。

那么,如何创建背景工作在合理的时间间隔中醒来呢?创建基于事件的通知的最佳方法是什么?我的想法是检查事件(例如,在数据库中检查新事物)以较小的时间间隔(15分钟也不是理想的),但是看到它与周期性工作不太合作,也是建议的根据文档的方法,我的选择到底是什么?

I've been using WorkManager to create notifications for my app. For my purposes I figured PeriodicWorkRequest is the most fitting, but after a bit of testing and reading online it's seems extremely unreliable. Using the minimal interval (15 minutes), and the app being closed, the worker woke up 5-6 times and then seems to be killed.

So how does one go about creating background work that wakes up in reasonable time intervals? What is the best approach for creating event-based notification? My idea was checking for the event (for example, checking for something new in the database) in small time intervals (with 15 minutes also being less than ideal), but seeing as it doesn't work well with PeriodicWorkRequest and is also the recommended approach as per the documentation, what exactly are my options?

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

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

发布评论

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

评论(1

小忆控 2025-02-05 01:50:49

基本上,Android的想法是为了您无法做您想做的事情,因为我们作为开发人员试图杀死电池。

您需要查看限制的演变是如何进行的:

版本6-打ze:

https://developer.android.com/training/monitoring-device-state/doze-standby

版本7另一种限制性限制的doge状态:
https://developer.android.com/android.com/about/versions/versions/versions/versions/ Nougat/Android-7.0-Changes#perf

广播限制:
https://developer.android.com/guide.com/guide/components/broadcasts

版本8.0背景执行限制:
https://developer.android.com/about.com/about.com/about/versions/oreo/oreo/background#服务

版本9备用存储桶 - 在其中取决于应用程序的使用方式,您可以使用不同的资源 - 例如时间唤醒应用程序,使用网络的时间等

https://developer.android.com/about/versions/pie/power#buckets
https://developer.android.android。 com/about/about/versions/12/candy-changes-all#jurdictive-app-standby bucket
https://develoveler.android.com/troid.com/topic/topic/performance/performance/appstandby

电池节省的改进 :

https://develpinger.android.com. android.com/about/about/about/pie/pie/pie/pie /Power#电池放电

电源管理限制 - 非常重要。
https://developer.android.com/topic/performance/power/power-详细信息

版本11和12 App Hibernation

https://developer.android.com /主题/绩效/应用程序hibrnation

长话短说 - 您需要防止所有这些限制损害您的工作。但是您需要遵守,因为它对用户来说更好。

,没有API会说 - “所有这些限制,并尽一切努力做。”

如果您需要确切的时机 - 您需要AlarmManager。

如果您不知道何时需要完成工作并依靠外部 - 推送通知,那可以将工作转移到工人。

如果您需要定期的不是时间关键时期的工作 - 您可能不会使用AlarmMangaer并确保完成工作已经完成,但是您无法确定何时,因为有很多限制,优先级将节省资源。

另外,您可以要求用户免于优化电池:

如果您想知道为什么不执行该工作,则需要检查JS限制并查看什么限制不满意:

Basically, the idea of Android is for you not to be able to do what you want to do because we as developers try to kill the battery.

You need to see how the evolution of the restrictions goes:

Version 6 - Doze:

https://developer.android.com/training/monitoring-device-state/doze-standby
https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-power

Version 7 Another state of Doze with even more restrictions:
https://developer.android.com/about/versions/nougat/android-7.0-changes#perf

Broadcast Restrictions:
https://developer.android.com/guide/components/broadcasts
https://developer.android.com/about/versions/nougat/android-7.0-changes#bg-opt

Version 8.0 Background execution limits:
https://developer.android.com/about/versions/oreo/background#services

Version 9 StandBy Buckets - where depending on how the app is used you have different resources to use - like time to wake up the app, time to use the Network, etc

https://developer.android.com/about/versions/pie/power#buckets
https://developer.android.com/about/versions/12/behavior-changes-all#restrictive-app-standby-bucket
https://developer.android.com/topic/performance/appstandby

Battery Save improvements:

https://developer.android.com/about/versions/pie/power#battery-saver

Power Management Restrictions - really important.
https://developer.android.com/topic/performance/power/power-details

Version 11 and 12 App hibernation

https://developer.android.com/topic/performance/app-hibernation

Long story short - you need to prevent all these restrictions to harm your work. But you need to comply because it is better for the user.

But there is no API that will just say - "f**k all these restrictions and do whatever the dev wants to do."

If you need exact timing - you need AlarmManager.

If you do not know when you need to do your work and depend on the outside - Push Notifications which then can transfer the work to the WorkManager.

If you need periodic work that is not time-critical - you might not use the AlarmMangaer and be sure that the work is finished, but you can't be sure when, because there are many restrictions and the priority will be saving the resources.

Also, you can ask the user to be exempted from Battery Optimization:

https://developer.android.com/training/monitoring-device-state/doze-standby#support_for_other_use_cases

If you want to know why exactly the work is not executed you need to check the JS dump and see what restriction is not satisfied:

https://developer.android.com/topic/libraries/architecture/workmanager/how-to/debugging#use-alb-shell0dumpsys-jobscheduler

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