如何创建 WorkerParameters [英] How can create WorkerParameters

查看:27
本文介绍了如何创建 WorkerParameters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Worker 的 Worker()@Deprecated ,所以需要使用

Worker's Worker() is @Deprecated , so need use

public Worker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

但是 WorkerParameters 的构造函数是 @hide.

but the WorkerParameters's constructor is @hide.

那么,如何创建 WorkerParameters 实例?

so, how can create WorkerParameters instance?

这个库是最新版的Android后台任务调度库,不过这个库最近更新了API,把核心类的旧创建方法标记为废弃

This library is the latest version of Android background task scheduling library, but this library has recently updated the API to mark the old creation method of the core class as discarded

现在,我不知道如何创建这个核心类,因为其中一个参数没有暴露构造函数,也没有找到 Factory/Builder 的工作方式

Now, I don't know how to create this core class, because one of the parameters doesn't expose the constructor, nor do I find the way Factory / Builder works

那么让我看看有没有人可以在伟大的 Stack Overflower 中帮助我.

So let me see if there is anyone who can help me in the great Stack Overflower.

核心类是Worker,params是WorkerParameters

The core class is Worker, params is WorkerParameters

推荐答案

好的,所以查询的是来自 WorkManager 的最新库版本(依赖: android.arch.work:work-runtime:1.0.0-alpha09),

Okay, so query for you is that from latest library version of WorkManager (dependency : android.arch.work:work-runtime:1.0.0-alpha09),

Theres 的构造函数从

Theres's constructor changed from

Worker()

Worker(@NonNull Context appContext, @NonNull WorkerParameters workerParams).

这意味着默认构造函数现在不再有用(已弃用,标记为应在未来版本中删除) &我们现在在参数化构造函数.

Which means that default constructor is now no longer useful (Deprecated, Marked as it should be removed on future versions) & we're now on parameterized constructor.

现在,如果您已经注意到,当我们使用 Worker 时,我们并没有直接为其创建新的 object,而是我们做这样的事情,

Now, if you've noticed that when we use our Worker, we're not directly creating new object of it, instead we do something like this,

OneTimeWorkRequest.from(FooWorker.class) // We pass Class<?> obj of our worker here, or in PeriodicWorkRequest-it's the same

(将我们Worker的类对象传递给我们的WorkRequest)

(Passing class Object of our Worker to our WorkRequest)

这意味着 WorkParameters 是由 DefaultWorkerFactory 内部提供的.所以你不必担心,因为我们不会自己创建 Worker 的新对象,除非你想要任何自定义设置(这个版本现在可以做到这一点,这很好).

此外,来自 2018 年 9 月 19 日

  • 您现在可以通过将 WorkerFactory 指定为 WorkManager.Configuration 的一部分,在运行时创建自己的 Worker 实例.回退工厂是 DefaultWorkerFactory,它匹配行为以前版本的 WorkManager.
  • WorkerNonBlockingWorker 的默认构造函数现在被标记为已弃用.请使用新的构造函数 (Worker(Context, WorkerParameters)) 并调用 super(Context, WorkerParameters); futureWorkManager 的版本将删除默认构造函数.
  • You can now create your own Worker instances at runtime by specifying a WorkerFactory as part of the WorkManager.Configuration. The fallback factory is DefaultWorkerFactory, which matches behavior of previous versions of WorkManager.
  • The default constructors for Worker and NonBlockingWorker are now marked as deprecated. Please use the new constructor (Worker(Context, WorkerParameters)) and call super(Context, WorkerParameters); future versions of WorkManager will remove the default constructor.

所以现在,这就是这个问题的全部原因,如果我想创建WorkerParameters怎么办?

So now, it's the reason this question is all about, that what if i want to create WorkerParameters?

答案是,在创建WorkerParameters之前,您需要覆盖WorkManager库的默认行为并提供您自己的自定义<代码>WorkManager.Configuration &通过在运行时通过向其提供 WorkerParameters 来创建您自己的 Worker.(在这种情况下,我们需要提供 WorkerParameters,如果您只是使用开箱即用的 WorkManager 功能,您可能不想这样做)

Answer is that before you can create WorkerParameters, you'll need to override default behaviour of WorkManager library and provide your own customized WorkManager.Configuration & by that creating your own Worker, at Runtime by providing WorkerParameters to it. (This is the case where we need to provide WorkerParameters, you might not wanna do that if you're just using WorkManager functionalities out of the box)

GoogleCodeLab 的示例精美地解释了 WorkManager(您可以在 此处查看 新构造函数是如何完成的).

There's GoogleCodeLab's example which beautifully explains WorkManager (You can see here how it's done with new constructor).

假设您有一个名为 FooWorkerWorker 类,那么您的代码将如下所示:

Let's say you have got a Worker class called FooWorker, then your code will look like this:

public class FooWorker extends Worker {
    public FooWorker(
            @NonNull Context appContext,
            @NonNull WorkerParameters workerParams) {
        super(appContext, workerParams);
    }

    private static final String TAG = FooWorker.class.getSimpleName();

    @NonNull
    @Override
    public Worker.Result doWork() {
        // Do some work & return appropriate result.
    }
}

并像这样初始化

//Init WorkManager
private WorkManager mWorkManager;
mWorkManager = WorkManager.getInstance();
// Enqueue our work to manager
mWorkManager.enqueue(OneTimeWorkRequest.from(FooWorker.class)); // Here, we're not creating new FooWorker(); & we don't need to worry about `WorkerParameters` for now.

所以没有必要访问/创建WorkerParamters构造函数/对象.

So there's no case to access/create constructor/object of WorkerParamters.

我希望这个回答能满足您的问题并有所帮助.

I hope this answer satisfies your question and helps.

这篇关于如何创建 WorkerParameters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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