简体   繁体   English

在Oreo及更高版本中使用WorkManager时,我应该显示正在进行的通知吗?

[英]Should I show ongoing notification when using WorkManager in Oreo and above?

When executing a task in the doWork() method of a Worker which runs in the background (for example, a worker which runs periodically in the background using PeriodicWorkRequest ), should I show an ongoing notification? 在后台运行的WorkerdoWork()方法中执行任务时(例如,使用PeriodicWorkRequest在后台周期性运行的worker),我应该显示正在进行的通知吗? If so, how do I go about it? 如果是这样,我该怎么做? Let's say I have something like: 假设我有类似的东西:

class NoticeWorker(context: Context, params: WorkerParameters)
    : Worker(context, params) {

    override fun doWork(): Result {
        // do some work here
        return Result.SUCCESS
    }

}

which is then executed with 然后执行

WorkManager.getInstance().enque(workerObj)

No, on Android version >= 23 (which includes Oreo), WorkManager will default to using the JobScheduler to actually enqueue and execute work. 不会,在Android版本> = 23(包括Oreo)上, WorkManager将默认使用JobScheduler实际入队并执行工作。 From the JobScheduler docs, we see that: JobScheduler文档中,我们看到:

While a job is running, the system holds a wakelock on behalf of your app. 在作业运行时,系统代表您的应用程序持有唤醒锁。 For this reason, you do not need to take any action to guarantee that the device stays awake for the duration of the job. 因此,您无需采取任何措施来保证设备在工作期间保持清醒状态。

The idea is that using WorkManager allows the system to decide when and how to do things in the background, which is good for performance, so they're trying to make it as easy as possible for us and all of this logic is abstracted away. 这个想法是,使用WorkManager可以使系统决定何时以及如何在后台执行操作,这对性能有好处,因此他们正努力使对我们来说尽可能简单,所有这些逻辑都被抽象了。 You only need to specify what needs to be done. 您只需要指定需要执行的操作即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM