对于像我这样的人,下面给出了答案:
这是Worker类:
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 {
private Model model;
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);
}
工人工厂:
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:
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();
}
Application类的一部分:
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 {
SimpleWorkerFactory factory();
}
应用程序清单的一部分(在应用程序内):
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove"/>
Gradle里有什么:
implementation ("android.arch.work:work-runtime:1.0.1")
顺便提一句,如果它与Firebase发生冲突
api 'com.google.guava:guava:27.1-android'
注意:在我的情况下,模型是通过接口注入的。例如:
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