问题在于我无法使用Java将带有dagger2的类注入到worker(WorkManager)中。
我试图在这里理解它的解释:https://proandroiddev.com/dagger-2-setup-with-workmanager-a-complete-step-by-step-guild-bb9f474bde37 而且我不知道为什么-但就我而言,这是行不通的。
public class SimpleWorker extends androidx.work.Worker {
private String TAG = "SimpleWorker";
SomeModel someModel; // this is injected model
public SimpleWorker(@NonNull Context context, @NonNull WorkerParameters
workerParams) {
super(context, workerParams);
}
@NonNull
@Override
public Result doWork() {
Log.d(TAG, someModel.toString()); // but here always null
return Result.success(); }
}
我希望它运行良好!
是-这是我的问题WorkManager Java Android Dagger2的重复 但是它被主持人关闭了,我没有时间回答。我真的很想为其他人节省时间。 ps。请-不要删除它。
答案 0 :(得分:2)
Artem Winokurov的回答很好。
我需要在BaseApp类中进行哪些更改
public class BaseApp implements HasAndroidInjector {
@Inject
SimpleWorkerFactory simpleWorkerFactory;
@Override
public void onCreate() {
super.onCreate();
AppInjector.init(this);
configureWorkManager();
}
private void configureWorkManager() {
Configuration config = new Configuration.Builder()
.setWorkerFactory(simpleWorkerFactory)
.build();
WorkManager.initialize(this, config);
}
答案 1 :(得分:0)
有像我这样的人的答案:
这里是Worker类:
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);
}
}
WorkerFactory:
/**
*
* @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;
}
CollectionUtils:
import dagger.Binds;
import dagger.Module;
import dagger.multibindings.IntoMap;
@Module
public interface WorkerBindingModule {
@Binds
@IntoMap
@WorkerKey(SimpleWorker.class)
ChildWorkerFactory bindHelloWorldWorker(SimpleWorker.Factory factory);
}
工作人员绑定:
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();
}
WorkerKey批注:
private static AppComponent component;
private void configureWorkManager() {
UpdaterWorkerFactory factory = component.factory();
Configuration config = new Configuration.Builder()
.setWorkerFactory(factory)
.build();
WorkManager.initialize(this, config);
}
Application类的一部分:
@Singleton
@Component(modules = {AppModule.class, WorkerBindingModule.class})
public interface AppComponent {
// Some other injects here
SimpleWorkerFactory factory();
}
AppComponent界面的一部分:
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove"/>
清单的一部分(在应用程序内部):
// (Java only)
implementation ("android.arch.work:work-runtime:1.0.1")
gradle中的内容是什么
api 'com.google.guava:guava:27.1-android'
ps。如果它会与firebase发生冲突
public class ModelImplementation implements Model {
private ModelImplementation() {
App.getComponent().inject(this);
}
}
注意:在我的情况下,模型被注入抛出接口。喜欢:
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(SimpleWorker.class,
Const.WORKER_PERIOD, TimeUnit.MINUTES).build();
WorkManager.getInstance().enqueue(periodicWorkRequest);
在同一个AppComponent中!
要使用此惊人功能,只需使用类似(例如在Activity中):
{{1}}
pps。 Const.WORKER_PERIOD-以分钟为单位的时间(至少15分钟)
targetSDK是27