You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级到WorkManager 2.7.0时如何为RxWorker实现getForegroundInfoAsync

RxWorker实现getForegroundInfoAsync适配加急WorkRequest方案

你遇到的报错是因为 Android 12+ 上的加急工作请求(Expedited WorkRequest)要求所有ListenableWorker子类(包括RxWorker)必须实现getForegroundInfoAsync方法,用于任务被调度到前台运行时提供所需的通知配置。


1. 关于Guava依赖的说明

  • WorkManager 2.7+ 版本已经内置传递了androidx.concurrent:futures依赖,官方推荐的Futures工具类、SettableFuture实现都在这个包下,不需要额外引入全量Guava库即可使用。
  • 除非你有其他Guava工具的使用需求,否则不需要单独添加Guava依赖。

2. RxWorker实现getForegroundInfoAsync的方案

分两种常用场景实现即可:

场景1:通知信息无需异步获取(绝大多数业务场景适用)

直接创建ForegroundInfo实例,用Futures.immediateFuture()直接包装返回即可,代码示例:

Kotlin 实现

// 注意导入androidx包下的工具类,不需要Guava
import androidx.concurrent.futures.immediateFuture
import androidx.work.ForegroundInfo
import androidx.work.RxWorker
import android.content.pm.ServiceInfo
import io.reactivex.rxjava3.core.Single

class MyCustomRxWorker(context: Context, params: WorkerParameters) : RxWorker(context, params) {

    // 重写加急任务要求的方法
    override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
        // 先构建你的通知实例,需提前完成Android 8.0+的通知渠道创建
        val notification = buildMyTaskNotification()
        // 参数说明:1.唯一通知ID 2.通知实例 3.可选:Android 12+要求的前台服务类型,按业务场景替换
        val foregroundInfo = ForegroundInfo(
            NOTIFICATION_ID,
            notification,
            ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
        )
        // 直接返回包装好的ListenableFuture
        return immediateFuture(foregroundInfo)
    }

    override fun createWork(): Single<Result> {
        // 保留你原有RxWorker的业务逻辑即可
        return yourBusinessTaskLogic()
    }

    companion object {
        private const val NOTIFICATION_ID = 1001
    }
}

Java 实现

import androidx.concurrent.futures.Futures;
import androidx.work.ForegroundInfo;
import androidx.work.RxWorker;
import android.content.pm.ServiceInfo;
import io.reactivex.rxjava3.core.Single;

public class MyCustomRxWorker extends RxWorker {
    private static final int NOTIFICATION_ID = 1001;

    public MyCustomRxWorker(Context context, WorkerParameters params) {
        super(context, params);
    }

    @NonNull
    @Override
    public ListenableFuture<ForegroundInfo> getForegroundInfoAsync() {
        Notification notification = buildMyTaskNotification();
        ForegroundInfo foregroundInfo = new ForegroundInfo(
                NOTIFICATION_ID,
                notification,
                ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
        );
        return Futures.immediateFuture(foregroundInfo);
    }

    @NonNull
    @Override
    public Single<Result> createWork() {
        // 保留你原有RxWorker的业务逻辑即可
        return yourBusinessTaskLogic();
    }
}

场景2:通知信息需要异步获取(比如需要读取本地存储的用户信息生成通知)

可以用SettableFuture包装RxJava的异步返回结果,不需要自己实现ListenableFuture:

override fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo> {
    val future = SettableFuture.create<ForegroundInfo>()
    // 你的异步获取通知信息的Single
    getAsyncNotificationInfo()
        .subscribe(
            { foregroundInfo -> future.set(foregroundInfo) },
            { throwable -> future.setException(throwable) }
        )
    return future
}

3. 额外注意事项

  • 确保已经在AndroidManifest.xml中声明了对应业务场景的前台服务权限,以及WorkManager 2.6+要求的加急任务相关配置。
  • 运行时要先完成通知渠道的创建,Android 8.0+未创建渠道会导致通知显示失败。
  • 加急任务的系统配额用完后会按你配置的RUN_AS_NON_EXPEDITED_WORK_REQUEST降级为普通任务,此时不会触发前台服务逻辑,不需要额外兼容。

内容的提问来源于stack exchange,提问作者Alvin Dizon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 12:06:04