И есть ответ для таких людей, как я:
Вот он, Рабочий класс:
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import androidx.work.ListenableWorker;
import androidx.work.WorkerParameters;
import javax.inject.Inject;
import javax.inject.Provider;
import com.sampleapp.model.Model;
public class SimpleWorker extends androidx.work.Worker {
//dagger (what we want to Inject into worker) U CAN ADD WHATEVER NEEDED
private Model model;
//not dagger (just some fields)
private String someField;
private final String TAG = getClass().getSimpleName();
private SimpleWorker(@NonNull Context context,
@NonNull WorkerParameters workerParams,
Model model) {
super(context, workerParams);
this.model = model;
someField = "just some work";
}
@NonNull
@Override
public ListenableWorker.Result doWork() {
Log.d(TAG, "Worker starts");
Log.d(TAG, model.getClass().getSimpleName() + " doing some work");
Log.d(TAG, "Job done!");
return ListenableWorker.Result.success();
}
public static class Factory implements ChildWorkerFactory {
private final Provider<Model> modelProvider;
@Inject
public Factory(Provider<Model> modelProvider) {
this.modelProvider = modelProvider;
}
@Override
public ListenableWorker create(Context context, WorkerParameters workerParameters) {
return new SimpleWorker(context,
workerParameters,
modelProvider.get());
}
}
}
Интерфейс:
public interface ChildWorkerFactory {
ListenableWorker create(Context appContext, WorkerParameters workerParameters);
}
WorkerFactory:
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
import androidx.work.ListenableWorker;
import androidx.work.WorkerFactory;
import androidx.work.WorkerParameters;
import com.sampleapp.model.Model;
import com.sampleapp.model.CollectionsUtil;
public class SimpleWorkerFactory extends WorkerFactory {
private final Map<Class<? extends ListenableWorker>, Provider<ChildWorkerFactory>> workersFactories;
@Inject
public SimpleWorkerFactory(Map<Class<? extends ListenableWorker>, Provider<ChildWorkerFactory>> workersFactories) {
this.workersFactories = workersFactories;
}
@Nullable
@Override
public ListenableWorker createWorker(@NonNull Context appContext, @NonNull String workerClassName, @NonNull WorkerParameters workerParameters) {
Provider<ChildWorkerFactory> factoryProvider = CollectionsUtil.getWorkerFactoryProviderByKey(workersFactories, workerClassName);
return factoryProvider.get().create(appContext, workerParameters);
}
}
CollectionUtils:
/**
*
* @param map workers
* @param key workers name (class name)
* @return
*/
public static Provider<ChildWorkerFactory> getWorkerFactoryProviderByKey(Map<Class<? extends ListenableWorker>, Provider<ChildWorkerFactory>> map, String key) {
for (Map.Entry<Class<? extends ListenableWorker>, Provider<ChildWorkerFactory>> entry : map.entrySet()) {
if (Objects.equals(key, entry.getKey().getName())) {
return entry.getValue();
}
}
return null;
}
Рабочая привязка:
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public interface WorkerBindingModule {
@Binds
@IntoMap
@WorkerKey(SimpleWorker.class)
ChildWorkerFactory bindHelloWorldWorker(SimpleWorker.Factory factory);
}
WorkerKey аннотация:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import androidx.work.ListenableWorker;
import dagger.MapKey;
@MapKey
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface WorkerKey {
Class<? extends ListenableWorker> value();
}
Часть класса приложения:
private static AppComponent component;
private void configureWorkManager() {
UpdaterWorkerFactory factory = component.factory();
Configuration config = new Configuration.Builder()
.setWorkerFactory(factory)
.build();
WorkManager.initialize(this, config);
}
Часть интерфейса AppComponent:
@Singleton
@Component(modules = {AppModule.class, WorkerBindingModule.class})
public interface AppComponent {
// Some other injects here
SimpleWorkerFactory factory();
}
И часть манифеста (внутри приложения):
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove"/>
Что было в gradle:
// (Java only)
implementation ("android.arch.work:work-runtime:1.0.1")
пс.И ЕСЛИ он получит некоторые конфликты с firebase
api 'com.google.guava:guava:27.1-android'
Примечание: в моем случае была введена модель интерфейса throw.например:
public class ModelImplementation implements Model {
private ModelImplementation() {
App.getComponent().inject(this);
}
}
В том же AppComponent!
, чтобы использовать эту удивительную функцию, просто используйте что-то вроде (в Activity например):
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SimpleWorker.class,
Const.WORKER_PERIOD, TimeUnit.MINUTES).build();
WorkManager.getInstance().enqueue(periodicWorkRequest);
pps.Const.WORKER_PERIOD - период в минутах (мин. 15)
targetSDK равен 27