Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can create WorkerParameters

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

like image 581
CaiJingLong Avatar asked Sep 20 '18 09:09

CaiJingLong


People also ask

How do you create a WorkManager?

To get started using WorkManager, first import the library into your Android project. Once you've added the dependencies and synchronized your Gradle project, the next step is to define some work to run.

How does a WorkManager work?

Android WorkManager is a background processing library which is used to execute background tasks which should run in a guaranteed way but not necessarily immediately. With WorkManager we can enqueue our background processing even when the app is not running and the device is rebooted for some reason.

How do you pass data to worker?

To pass an argument to a task, call the WorkRequest. Builder. setInputData(Data) method before you create the WorkRequest object. That method takes a Data object, which you create with Data.

How do you pass parameters to WorkManager?

public static OneTimeWorkRequest create(String id) { Data inputData = new Data. Builder() . putString(TASK_ID, id) . build(); return new OneTimeWorkRequest.


1 Answers

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.

like image 129
Jeel Vankhede Avatar answered Sep 17 '22 18:09

Jeel Vankhede