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

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

问题描述

Worker的 Worker() @Deprecated ,因此需要使用

  public Worker(@NonNull上下文上下文,@ NonNull WorkerParameters workerParams){超级(上下文,workerParams);} 

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

那么,如何创建WorkerParameters实例?


此库是Android后台任务调度库的最新版本,但该库最近更新了API,以将核心类的旧创建方法标记为已废弃

现在,我不知道如何创建此核心类,因为其中一个参数不会公开构造函数,也无法找到Factory/Builder的工作方式

所以,让我看看是否有人可以帮助我完成Stack Overflower.

核心类是Worker,参数是WorkerParameters

解决方案

好的,所以您需要查询的是最新版本的 WorkManager (依赖项): android.arch.work:work-runtime:1.0.0-alpha09 )

Theres的构造函数从

更改

Worker()

Worker(@NonNull上下文appContext,@ NonNull WorkerParameters workerParams) .

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

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

  OneTimeWorkRequest.from(FooWorker.class)//我们通过Class<?>这里或在PeriodicWorkRequest中我们工人的obj相同 

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

这意味着 WorkParameters DefaultWorkerFactory 内部提供的.因此,您不必担心,因为,除非您需要任何自定义的设置(这个版本现在可以实现了,而且很好).


此外,根据 2018年9月19日的最新API更改

  • 您现在可以在运行时通过指定 WorkerFactory 作为 WorkManager.Configuration 的一部分来创建自己的 Worker 实例.后备工厂为 DefaultWorkerFactory ,与行为匹配 WorkManager 的早期版本.
  • Worker NonBlockingWorker 的默认构造函数现已标记为已弃用.请使用新的构造函数(Worker(Context,WorkerParameters))并调用 super(Context,WorkerParameters); future版本的 WorkManager 将删除默认的构造函数.

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

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


有GoogleCodeLab的 示例 漂亮地解释了 WorkManager (您可以在 此处查看 如何使用新的构造函数.

结论:

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

 公共类FooWorker扩展Worker {公共FooWorker(@NonNull上下文appContext,@NonNull WorkerParameters workerParams){超级(appContext,workerParams);}私有静态最终字符串TAG = FooWorker.class.getSimpleName();@NonNull@Overridepublic Worker.Result doWork(){//做一些工作返回适当的结果.}} 

并像这样初始化

 //Init WorkManager私人WorkManager mWorkManager;mWorkManager = WorkManager.getInstance();//将我们的工作交给经理mWorkManager.enqueue(OneTimeWorkRequest.from(FooWorker.class));//在这里,我们没有创建新的FooWorker();&我们现在不必担心`WorkerParameters`. 

因此,在任何情况下都无法访问/创建 WorkerParamters 构造器/对象.

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

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

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

but the WorkerParameters's constructor is @hide.

so, how can create WorkerParameters instance?


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

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

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

The core class is Worker, params is WorkerParameters

解决方案

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

Theres's constructor changed from

Worker()

to

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.

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

(Passing class Object of our Worker to our WorkRequest)

this means that WorkParameters are something that is provided by DefaultWorkerFactory internally. So you don't have to worry about it because, we're not going to create new object of Worker by ourself, unless you want any customized setup (Which is now possible by this version & it's good).


Also, from latest API changes on September 19, 2018

  • 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.

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

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)


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

Conclusion:

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.
    }
}

and initialize like this

//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.

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天全站免登陆