您如何实现DaggerService

编程入门 行业动态 更新时间:2024-10-10 04:26:19
本文介绍了您如何实现DaggerService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经看过基础知识和课程,但是对匕首(甚至是匕首2)不熟悉,我不知道该怎么使用

I've looked at the basics as well as the class, but being new to dagger (or even dagger 2) I have no idea how I'm suppose to use this

这是匕首意图服务: google .github.io/dagger/api/latest/dagger/android/DaggerIntentService.html

我了解android意向服务和实现的基础知识,但是我似乎无法在DaggerIntentService上找到信息(而且我也很难为DaggerService查找信息)

I understand the android intent service and the basics for implementing, but I can't seem to find info over the DaggerIntentService (and I also struggle at finding info for DaggerService)

我的目标是使用TDD进行构建,但是我真的只需要了解实现基于dagger的服务的工作流程

My goal is to build it using TDD, but I really just need to understand the workflow for implementing a dagger based service

谢谢, 凯利

推荐答案

这故意不能解决DaggerIntentService问题.通过为Intent服务设置相关的组件和模块,dagger.android软件包或多或少地执行与可以手动执行的相同的操作.因此,您可以尝试以下方法:

This does not answer the DaggerIntentService problem, on purpose. dagger.android packages more or less do the same thing you can do manually by setting relevant component and module for the Intent service. So you might try following approach:

ServiceComponent

@Subcomponent(modules = ServiceModule.class) public interface ServiceComponent{ @Subcomponent.Builder public interface Builder { Builder withServiceModule(ServiceModule serviceModule); ServiceComponent build(); } void inject(MyService myService); }

ServiceModule

@Module public class ServiceModule{ private MyService myService; public ServiceModule(MyService myService){ this.myService = myService; } @Provides public MyService provideServiceContext(){ return myService; } @Provides public SomeRepository provideSomeRepository(){ return new SomeRepository(); } }

请记住,您具有根匕首组件,例如您在应用程序onCreate()方法中实例化的ApplicationComponent,在应用程序类中将需要其他公共方法.

Having in mind that you have root dagger component, for example ApplicationComponent which you instantiate in application onCreate() method, you'll need an additional public method in your application class.

ApplicationComponent

@Component(modules = ApplicationModule.class) public interface ApplicationComponent{ ServiceComponent.Builder serviceBuilder(); }

ApplicationModule

@Module(subcomponents = ServiceComponent.class) public class ApplicationModule{ public ApplicationModule(MyApplication myApplication){ this.myApplication = myApplication; } @Provides public MyApplication providesMyApplication(){ return myApplication; } }

MyApplication

public class MyApplication extends Application{ ApplicationComponent applicationComponent; @Override public void onCreate(){ super.onCreate(); applicationComponent = DaggerApplicationComponent.builder() .applicationModule(new ApplicationModule(this)) .build(); } public ServiceComponent getServiceInjector(MyService myService){ return applicationComponent.serviceBuilder().withServiceModule(new ServiceModule(myService)).build(); }

最后,您的MyService:)

MyService

public class MyService extends IntentService{ @Inject MyApplication application; @Inject SomeRepository someRepository; public onCreate(){ ((MyApplication)getApplicationContext()).getServiceInjector(this).inject(); } public void onHandleIntent(Intent intent){ //todo extract your data here }

乍一看可能很复杂,但是如果您已经设置了匕首结构,那么只需增加两到三个类即可.

It might look complicated at first, but if you have dagger structure setup already, then its just two to three additional classes.

希望对您有所帮助. 干杯.

Hope you find it helpful. Cheers.

更多推荐

您如何实现DaggerService

本文发布于:2023-11-23 07:29:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   DaggerService

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!