简体   繁体   English

Android Dagger 2.11中的BroadcastReceiver

[英]BroadcastReceiver in Android dagger 2.11

I m trying to inject broadcast receiver with dagger in android. 我试图在Android中使用匕首注入广播接收器。 I m using dagger 2.11. 我正在使用匕首2.11。 I have BroadcastReceiver module class, component class, and I implement HasBroadcastReceiverInjector in Application class. 我有BroadcastReceiver模块类,组件类,并且在Application类中实现了HasBroadcastReceiverInjector。 Do I have to implement HasBroadcastReceiverInjector in Application class, or I can implement HasBroadcastReceiverInjector in Activity class. 我是否必须在Application类中实现HasBroadcastReceiverInjector,或者可以在Activity类中实现HasBroadcastReceiverInjector。 Has anybody successed to inject BroadcastReceiver with dagger, or someone knows what am I doing wrong 是否有人成功用匕首注入BroadcastReceiver,或者有人知道我在做什么错

Here is my BroadcastReceiver module class 这是我的BroadcastReceiver模块类

@Subcomponent(modules = LikeBroadcastReceiverModule.class)
public interface LikeBroadcastReceiverComponent extends AndroidInjector<LikeBroadcaseReceiver> {

@Subcomponent.Builder
abstract class Builder extends AndroidInjector.Builder<LikeBroadcaseReceiver>{}}

Here is my BroadcastReceiver module class 这是我的BroadcastReceiver模块类

@Module
public class LikeBroadcastReceiverModule {

@Provides
public LikeBroadcastReceiverView provideBroadcastView(LikeBroadcaseReceiver likeBroadcaseReceiver){
    return likeBroadcaseReceiver;
}

@Provides
public BroadcastReceiverPresenter providePresenter(){
    return new BroadcastReceiverPresenterImpl();
}}

Here is my Application class 这是我的应用程序类

public class EasyDriveApplication extends Application implements HasActivityInjector, HasBroadcastReceiverInjector{

@Inject
DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;

@Inject
DispatchingAndroidInjector<BroadcastReceiver> broadcastReceiverDispatchingAndroidInjector;

@Override
public void onCreate() {
    super.onCreate();
    //Fabric.with(this, new Crashlytics());
    DaggerAppComponent.builder().application(this).build().inject(this);
}

@Override
public AndroidInjector<Activity> activityInjector() {
    return activityDispatchingAndroidInjector;
}

@Override
public AndroidInjector<BroadcastReceiver> broadcastReceiverInjector() {
    return broadcastReceiverDispatchingAndroidInjector;
}}

and here is my AppComponent class 这是我的AppComponent类

@Singleton
@Component(modules = {
    AndroidInjectionModule.class,
    AppModule.class,
    DaoModule.class,
    MapperModule.class,
    UseCaseModule.class,
    ThreadingModule.class,
    UtilsModule.class,
    BroadcastReceiverBuilder.class,
    ActivityBuilder.class
    })
public interface AppComponent {

@Component.Builder
interface Builder {
    @BindsInstance
    Builder application(Application application);
    AppComponent build();
}

void inject(EasyDriveApplication app);}

and my BroadcastReceiverBuilder class is looks like 我的BroadcastReceiverBuilder类看起来像

@Module
public abstract class BroadcastReceiverBuilder {

@Binds
@IntoMap
@BroadcastReceiverKey(LikeBroadcaseReceiver.class)
abstract AndroidInjector.Factory<? extends BroadcastReceiver> bindLikeBroadcastReceiver(LikeBroadcastReceiverComponent.Builder builder);}

This is error I get 这是我得到的错误

Error:(46, 10) error: com.presentation.dagger.broadcastReceiver.LikeBroadcastReceiverComponent.Builder cannot be provided without an @Provides-annotated method. 错误:(46,10)错误:如果没有@Provides批注的方法,就无法提供com.presentation.dagger.broadcastReceiver.LikeBroadcastReceiverComponent.Builder。 com.presentation.dagger.broadcastReceiver.LikeBroadcastReceiverComponent.Builder is injected at com.presentation.dagger.application.module.BroadcastReceiverBuilder.bindLikeBroadcastReceiver(builder) java.util.Map,javax.inject.Provider>> is injected at dagger.android.DispatchingAndroidInjector.(injectorFactories) dagger.android.DispatchingAndroidInjector is injected at com.presentation.dagger.application.EasyDriveApplication.broadcastReceiverDispatchingAndroidInjector com.presentation.dagger.application.EasyDriveApplication is injected at com.presentation.dagger.application.AppComponent.inject(app) com.presentation.dagger.broadcastReceiver.LikeBroadcastReceiverComponent.Builder注入com.presentation.dagger.application.module.BroadcastReceiverBuilder.bindLikeBroadcastReceiver(builder)java.util.Map,javax.inject.Provider >>注入dagger.android。 DispatchingAndroidInjector。(injectorFactories)dagger.android.DispatchingAndroidInjector在com.presentation.dagger.application.EasyDriveApplication.broadcastReceiverDispatchingAndroidInjector com.presentation.dagger.application.EasyDriveApplication注入在com.presentation.dagger.application.app.AppComponent.in

I found this page whilst trying to find a way of dependency injecting into a BroadcastReceiver, for anyone else who hits on an issue here is how to do it 我在尝试找到一种将依赖项注入到BroadcastReceiver中的方法时找到了此页面,对于在此遇到问题的其他任何人,该如何做

@Provides
fun providesBlah() : Blah = Blah()

class BlahBroadcastReceiver : BroadcastReceiver() {

@Inject
lateinit var blah: Blah

override fun onReceive(context: Context, intent: Intent) {
    (context.applicationContext as MyApplication).component().inject(this)
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM