Expedited WorkRequests 需要一个 ListenableWorker 来提供 getForegroundInfoAsync() 的实现 [英] Expedited WorkRequests require a ListenableWorker to provide an implementation for getForegroundInfoAsync()

查看:155
本文介绍了Expedited WorkRequests 需要一个 ListenableWorker 来提供 getForegroundInfoAsync() 的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

做一点危险风格问答 在这里.

我有一些工作有时需要以加速运行,如WorkManager 2.7.0 版:

I have some work I sometimes need to run as expedited as described in the version 2.7.0 of WorkManager:

val constraints = Constraints.Builder()
    .setRequiredNetworkType(NetworkType.CONNECTED).build()
val oneTimeWorkRequest = OneTimeWorkRequest.Builder(MyWorker::class.java)
    .setInitialDelay(2, TimeUnit.SECONDS)
    .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
    .setConstraints(constraints).build()
WorkManager.getInstance(context).enqueueUniqueWork("my-identifier", ExistingWorkPolicy.REPLACE, oneTimeWorkRequest)

我相信代码在 Android 12/S 上运行得很好,但是当作业在 Android 11 上运行时,我收到以下错误:

I believe the code ran just fine on Android 12/S, but when the job is run on Android 11 I get the following error:

E/WM-WorkerWrapper: Work [ id=<UUID>, tags={ [WorkerTag] } ] failed because it threw an exception/error
     java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.util.concurrent.ExecutionException: java.lang.IllegalStateException: Expedited WorkRequests require a ListenableWorker to provide an implementation for `getForegroundInfoAsync()`
        at androidx.work.impl.utils.futures.AbstractFuture.getDoneValue(AbstractFuture.java:516)
        at androidx.work.impl.utils.futures.AbstractFuture.get(AbstractFuture.java:475)
        at androidx.work.impl.WorkerWrapper$2.run(WorkerWrapper.java:311)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:923)

我需要做什么?

推荐答案

ListenableWorker.getForegroundInfoAsync() 的文档说明了这一点:

The documentation for ListenableWorker.getForegroundInfoAsync() states this:

在 Android S 之前,WorkManager 代表您管理和运行前台服务以执行 WorkRequest,显示 ForegroundInfo 中提供的通知.要随后更新此通知,应用程序可以使用 NotificationManager.

Prior to Android S, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, showing the notification provided in the ForegroundInfo. To update this notification subsequently, the application can use NotificationManager.

从 Android S 及更高版本开始,WorkManager 使用即时作业管理此 WorkRequest.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

因此在扩展 ListenableWorker 的类中,有必要覆盖 getForegroundInfoAsync().

So in the class extending ListenableWorker it's necessary to override getForegroundInfoAsync().

自己直接覆盖该方法的替代方法是使用例如 CoroutineWorker:

An alternative to directly overriding that method yourself is to use for example CoroutineWorker:

class MyWorker(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {

override suspend fun doWork(): Result {
    // TODO: Do work here
    return Result.success()
}

override suspend fun getForegroundInfo(): ForegroundInfo {
    val notification = NotificationCompat.Builder(context, "some-channel-id")
        .setContentIntent(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), Constants.PENDING_INTENT_FLAG_IMMUTABLE))
        .setSmallIcon(R.drawable.ic_refresh_24dp)
        .setOngoing(true)
        .setAutoCancel(true)
        .setOnlyAlertOnce(true)
        .setPriority(NotificationCompat.PRIORITY_MIN)
        .setContentTitle(context.getString(R.string.app_name))
        .setLocalOnly(true)
        .setVisibility(NotificationCompat.VISIBILITY_SECRET)
        .setContentText("Updating widget")
        .build()
    return ForegroundInfo(1337, notification)
}

}

(挂起 Intent 标志的常量实际上只是 val PENDING_INTENT_FLAG_MUTABLE = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0 使事情与Android 12/S 及更早版本.)

(That constant for the pending intent flag is really just val PENDING_INTENT_FLAG_MUTABLE = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) PendingIntent.FLAG_MUTABLE else 0 to make stuff work with both Android 12/S and earlier.)

这篇关于Expedited WorkRequests 需要一个 ListenableWorker 来提供 getForegroundInfoAsync() 的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆