gpt4 book ai didi

android - 加急工作请求需要 ListenableWorker 来提供 getForegroundInfoAsync() 的实现

转载 作者:行者123 更新时间:2023-12-04 23:55:48 25 4
gpt4 key购买 nike

做一点Jeopardy style Q&A这里。
我有一些工作有时需要按照 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 上运行时,我收到以下错误:
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)
我需要做什么?

最佳答案

The documentation for ListenableWorker.getForegroundInfoAsync() 指出:

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.

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


所以在类扩展 ListenableWorker有必要覆盖 getForegroundInfoAsync() .
自己直接覆盖该方法的另一种方法是使用例如 CoroutineWorker :
    class MyWorker(val context: Context, workerParams: WorkerParameters) : CoroutineWorker(context, workerParams) {

companion object {
private const val NOTIFICATION_CHANNEL_ID = "11"
private const val NOTIFICATION_CHANNEL_NAME = "Work Service"
}

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

override suspend fun getForegroundInfo(): ForegroundInfo {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
NOTIFICATION_CHANNEL_ID,
NOTIFICATION_CHANNEL_NAME,
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}

val notification = NotificationCompat.Builder(context, NOTIFICATION_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 及更早版本。)

关于android - 加急工作请求需要 ListenableWorker 来提供 getForegroundInfoAsync() 的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69627330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com