WorkerFactory
abstract class WorkerFactory
kotlin.Any | |
↳ | androidx.work.WorkerFactory |
A factory object that creates ListenableWorker
instances. The factory is invoked every time a work runs. You can override the default implementation of this factory by manually initializing WorkManager
(see WorkManager#initialize(Context, Configuration)
and specifying a new WorkerFactory in Configuration.Builder#setWorkerFactory(WorkerFactory)
.
Summary
Public constructors |
|
---|---|
<init>() A factory object that creates |
Public methods |
|
---|---|
abstract ListenableWorker? |
createWorker(@NonNull appContext: Context, @NonNull workerClassName: String, @NonNull workerParameters: WorkerParameters) Override this method to implement your custom worker-creation logic. |
Public constructors
<init>
WorkerFactory()
A factory object that creates ListenableWorker
instances. The factory is invoked every time a work runs. You can override the default implementation of this factory by manually initializing WorkManager
(see WorkManager#initialize(Context, Configuration)
and specifying a new WorkerFactory in Configuration.Builder#setWorkerFactory(WorkerFactory)
.
Public methods
createWorker
@Nullable abstract fun createWorker(@NonNull appContext: Context, @NonNull workerClassName: String, @NonNull workerParameters: WorkerParameters): ListenableWorker?
Override this method to implement your custom worker-creation logic. Use Configuration.Builder#setWorkerFactory(WorkerFactory)
to use your custom class.
Exception
here will crash the application. If a WorkerFactory
is unable to create an instance of the ListenableWorker
, it should return null
so it can delegate to the default WorkerFactory
.
Returns a new instance of the specified workerClassName
given the arguments. The returned worker must be a newly-created instance and must not have been previously returned or invoked by WorkManager. Otherwise, WorkManager will throw an IllegalStateException
.
Parameters | |
---|---|
appContext |
Context: The application context |
workerClassName |
Context: The class name of the worker to create |
workerParameters |
Context: Parameters for worker initialization |
Return | |
---|---|
ListenableWorker?: A new ListenableWorker instance of type workerClassName , or null if the worker could not be created |