WorkManager Java Android Dagger2 [英] WorkManager Java Android Dagger2

查看:121
本文介绍了WorkManager Java Android Dagger2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要仅使用JAVA在Android应用中使用Workmanager(无需Kotlin !!!)。
在一个项目中,我们仅使用dagger2.8(不使用android-dagger!),我需要注入或访问一些Injected类,例如DataBase。 (它注入了AppComponent中。)

I NEED working Workmanager in Android app with JAVA only (without Kotlin!!!). In a project, we use just dagger2.8 (without android-dagger!) and I need to inject or access to some Injected classes like DataBase. (it injected in AppComponent).

已经尝试过:
https://proandroiddev.com/dagger-2-setup-with-workmanager-a-complete-by-step -guild-bb9f474bde37
用kotlin并用Java重写。 -在项目中无效。
(以及StackOverflow上的所有接近标题)

Already tried: https://proandroiddev.com/dagger-2-setup-with-workmanager-a-complete-step-by-step-guild-bb9f474bde37 in kotlin and rewrite in Java. - there is doesn't work in a project. (and all close titles on StackOverflow)

还有很多围绕Web的示例相互复制...(它们都在Kotlin中... )

And a lot of examples around web copied to each other...(all of them in Kotlin...)

我将所有库更新为实际版本,从3.2.1升级到3.4.1,并包含所有隐含的内容。

I update all libraries to actual versions, update as from 3.2.1 to 3.4.1 with everything that implies.

使用:AS 3.4.1 | gp:3.4.1 | gv:5.1.1库:Dagger2,Retrofit2,OkHttp,Gson,ButterKnife,RxJava2,Room,Rabbit,WorkManager。在28 sdk
上有我的工作人员:

Use: AS 3.4.1 | gp:3.4.1 | gv: 5.1.1 libraries: Dagger2, Retrofit2, OkHttp, Gson, ButterKnife, RxJava2, Room, Rabbit, WorkManager. And it is on 28 sdk There is my worker:

private String TAG = getClass().getSimpleName();

DBModel dbModel; // this is injected room model in appComponent

public UIUpdaterWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
    super(context, workerParams);
}

@NonNull
@Override
public Result doWork() {
    Log.d(TAG, dbModel.toString()); // here is null
    return Result.success();

我在同一组件中尝试并分开进行了测试。而且它行不通。具有null或项目无法生成。

I tried it in the same component and separate. And it won't work. Have null or project doesn't build.

推荐答案

如您链接的文章中所述,您需要使用 WorkManager的自定义配置,带有自定义WorkerFactory,该自定义WorkerFactory将附加参数传递给您的Worker类:​​

As described in the article you linked, you need to use a WorkManager's custom configuration with a custom WorkerFactory that pass an additional parameter to your Worker class:

public class UIUpdaterWorker extends Worker {
    private String TAG = getClass().getSimpleName();

    public UIUpdaterWorker(
            @NonNull Context context,
            @NonNull WorkerParameters workerParams,
            @NonNull DBModel dbModel
    ) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.d(TAG, dbModel.toString());
        return Result.success();
    }
}

然后可以用Dagger( DBModel )。

You can then injected this with Dagger (DBModel in your case).

我喜欢在WorkManager中添加的新`DelegatingWorkerFactory'功能 2.1.0-alpha02 (或者您也可以在类似的设计上做出自己的选择) 。我创建了一个PoC(在Kotlin中),该PoC创建了一个自定义配置,并为需要使用dagger注入额外参数的特定工人注册了WorkerFactory。

I like the new `DelegatingWorkerFactory' functionality added in WorkManager 2.1.0-alpha02 (or you can roll your own take on a similar design). I made a PoC (in Kotlin) that create a custom configuration and register a WorkerFactory for a specific worker that needs an additional parameter injected with dagger.

您可以找到代码在此格子分支,Worker和WorkerFactory是此处。为了提供有关您的特定案例的更多指导,可能需要更多的上下文。我可以想象您已经在其他类中注入了dbModel。

You can find the code on this Plaid branch, Worker and WorkerFactory are here. To provide more guidance on your specific case it would be helpful to have some more context. I can imagine that you are already injecting the dbModel in other classes.

注册自定义WorkerFactory时,可以执行相同的操作,注入dbModel,这样您就可以将其作为参数传递给Worker类。

You can do the same when you register your custom WorkerFactory, injecting the dbModel, so that you can then pass that as a parameter to the Worker class.

这篇关于WorkManager Java Android Dagger2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆