我必须创建一个应用程序,在一天中的某些时间激活接近警报(这段时间可能长达几个小时)。 即使应用程序从未打开过,接近警报也必须自动启动。
我的问题是:在这种情况下我可以使用 WorkManager
吗?或者我应该依靠前台服务
?
最佳答案
WorkManager 2.3.0-alpha02 adds built-in support for long running workers. In such cases, WorkManager can provide a signal to the OS that the process should be kept alive if possible while this work is executing. These Workers can run longer than 10 minutes. Example use-cases for this new feature include bulk uploads or downloads (that cannot be chunked), crunching on an ML model locally, or a task that's important to the user of the app.
在底层,WorkManager 代表您管理和运行前台服务以执行 WorkRequest,同时还显示可配置的通知。
ListenableWorker 现在支持 setForegroundAsync() API,CoroutineWorker 支持挂起的 setForeground() API。这些 API 允许开发人员指定此 WorkRequest 是重要的(从用户角度来看)还是长期运行的。
从 2.3.0-alpha03 开始,WorkManager 还允许您创建 PendingIntent,它可用于取消工作线程,而无需使用 createCancelPendingIntent() API 注册新的 Android 组件。此方法在与 setForegroundAsync() 或 setForeground() API 一起使用时特别有用,可用于添加取消 Worker 的通知操作。
资源链接:https://developer.android.com/topic/libraries/architecture/workmanager/advanced/long-running
关于android - 是否建议使用 Android WorkManager 来执行长任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72230952/