android - 是否建议使用 Android WorkManager 来执行长任务?

标签 android android-service android-workmanager android-geofence

我必须创建一个应用程序,在一天中的某些时间激活接近警报(这段时间可能长达几个小时)。 即使应用程序从未打开过,接近警报也必须自动启动。

我的问题是:在这种情况下我可以使用 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/

相关文章:

android - 使用 enqueueUniquePeriodicWork 的 PeriodicWork 不起作用

Android 位图解码和缩放质量损失

android - 除了使用 Intent 过滤器之外,我还可以过滤 BroadcastReceiver 吗?

android - 将多个图像上传到队列中的服务器

Android WorkManager - CoroutineWorker : Overriding coroutineContext is deprecated

Android 从 WorkManager 队列中删除工作人员或将状态从 FAILED 更改为 CANCELED

android - 如何使用 Android-NDK 直接从 native 代码访问资源(如声音、图像等)?

android - 通过 ADB Android 发送 AT 命令

android - 如何监控 Android 设备中应用程序的后台服务?

java - 带有传感器数据的 Android Service 中的数据并发