How to inject into a BroadcastReceiver

Does someone already had to inject an already existing class, with some business logic, into a BroadcastReceiver using dagger?

I'm using dagger 1 and already found a nice example (https://github.com/adennie/fb-android-dagger) but, I could not find how we can add an already existing class, which belongs to a different module, into a BroadcastReceiver.

Any help or advice would be greatly appreciated.


Same as you inject to an Activity

public void onReceive(Context context, Intent intent) {
        ((Application) context.getApplicationContext()).getInjector().inject(this);
    }

<Summary Topics/>[01] Dagger2.11 BroadcastReceiver Injection , The modules we call in the component are created at compile time and written to our graph.To be able to inject dependency into the broadcast  It might be too late to answer this question, but I will provide an example from my recent project where I tried to inject AppWidgetProvider which is a direct subclass of BroadcastReceiver. We need to inject a retrofit service into a BroadcastReceiver: @Module public class NetModule { /** shrunk for simplicity's sake.


It might be too late to answer this question, but I will provide an example from my recent project where I tried to inject AppWidgetProvider which is a direct subclass of BroadcastReceiver.

We need to inject a retrofit service into a BroadcastReceiver:

@Module
public class NetModule {
    /** shrunk for simplicity's sake. **/
    @Singleton
    @Provides
    public WidgetService provideWidgetService(Application application, OkHttpClient client, Gson gson) {
        return new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(application.getString(R.string.api_url))
                .client(client)
                .build()
                .create(WidgetService.class);
    }
}

Create another abstract @Module for the with abstract methods annotated with @ContributesAndroidInjector that return BroadcastReceivers you want to inject:

/**
 * To inject the app widgets.
 */
@Module
public abstract class WidgetsModule {
    @ContributesAndroidInjector
    abstract IngredientsWidget contributesIngredientsWidget();
}

If you forgot to add this module, you will get error like:

java.lang.IllegalArgumentException: No injector factory bound for Class<>

Then the component with both modules, besides AndroidInjectionModule

@Singleton
@Component(modules = {AndroidInjectionModule.class, NetModule.class, WidgetsModule.class})
public interface AppComponent {
    void inject(RecipesApp recipesApp);
}

Then in your Application class you implement HasBroadcastReceiverInjector interface.

public class RecipesApp extends Application implements HasBroadcastReceiverInjector {

    @Inject
    DispatchingAndroidInjector<BroadcastReceiver> broadcastReceiverInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        component().inject(this);
    }

    public AppComponent component() {
        return DaggerAppComponent.builder()
                .build();
    }

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

Finally, you can inject your BroadcastReceiver inside onReceive() before calling the super().

public class IngredientsWidget extends AppWidgetProvider {

    @Inject
    public WidgetService api;


    @Override
    public void onReceive(Context context, Intent intent) {
        /** Don't forget this line **/
        AndroidInjection.inject(this, context);
        super.onReceive(context, intent);
    }

}

You can find more about how to inject android components docs.

I built a small sample: broadcast-injection.

Your First Injected Service and BroadcastReceiver, Inject support for BroadcastReceiver #17. Closed. dkrivoruchko opened this issue on Nov 8, 2017 · 5 comments. Closed  Creating a BroadcastReceiver Let’s quickly implement a custom BroadcastReceiver as shown below. public class MyReceiver extends BroadcastReceiver { public MyReceiver() { } @Override public void onReceive(Context context, Intent intent) { Toast.makeText (context, "Action: " + intent.getAction (), Toast.LENGTH_SHORT).show (); } }


Dagger 2 example for injecting objects into a BroadcastReceiver.

The BroadcastReceiverModule.kt

@Module
abstract class BroadcastReceiverModule {
    @ContributesAndroidInjector
    abstract fun contributesMyTestReceiver() : MyTestReceiver
}

The AppComponent.kt

@Singleton
@Component(
        modules = [
            (AndroidSupportInjectionModule::class),
            (BroadcastReceiverModule::class)
        ])
interface AppComponent : AndroidInjector<MyApp> {
    @Component.Builder
    abstract class Builder : AndroidInjector.Builder<MyApp>()
}

The Application class

class MyApp : DaggerApplication() {
    override fun applicationInjector(): AndroidInjector<MyApp> =
            DaggerAppComponent.builder().create(this@MyApp)
}

The BroadcastReceiver class

class MyTestReceiver : BroadcastReceiver() {

    @Inject
    lateinit var anInjectedObject: MyInjectObject

    override fun onReceive(context: Context, intent: Intent) {
        AndroidInjection.inject(this, context)
        anInjectedObject.doSomthing()
    }
}

Inject support for BroadcastReceiver · Issue #17 · InsertKoinIO/koin , If, instead, the BroadcastReceiver is created in code, prefer constructor injection. Note: this class is not thread safe and should not be used with multiple Handler s​  I m trying to inject broadcast receiver with dagger in android. I m using dagger 2.11. I have BroadcastReceiver module class, component class, and I implement HasBroadcastReceiverInjector in Applic


I managed to inject use cases into my Broadcast by defining a Module which provide the use cases I need and I add the Module on the onReceive method, check the code below:

My BroadcastReceiverModule:

@Module(injects = { MyBroadcastReceiver.class }, addsTo = MyAppModule.class)
public class BroadcastReceiverModule {
    @Provides @Singleton MyUtilsClass providesMyUtilsClass(MyUtilsClassImpl myUtilsClass) {
        return myUtilsClass;
    }
    @Provides @Singleton MyUseCase providesMyUseCase(MyUseCaseImpl myUseCaseUtils) {
        return myUseCaseUtils;
    }
}

My BroadCastReceiver:

@Inject MyUtilsClass myUtilsClass;
@Inject MyUseCase myUseCase;
@Override public void onReceive(Context context, Intent intent) {
    AcidApplication.getScopedGraph(getModules().toArray()).inject(this);
    myUseCase.call();
    myUtilsClass.doSomething();
}
protected List<Object> getModules() {
    List<Object> result = new ArrayList<>();
    result.add(new BroadcastReceiverModule());
    return result;
}

DaggerBroadcastReceiver, If you declare a broadcast receiver in your manifest, the system Subclass BroadcastReceiver and implement onReceive(Context, Intent) . Broadcast Receivers in Xamarin.Android. 04/20/2018; 8 minutes to read +3; In this article. This section discusses how to use a Broadcast Receiver. Broadcast Receiver Overview. A broadcast receiver is an Android component that allows an application to respond to messages (an Android Intent) that are broadcast by the Android operating system or


You can use DaggerBroadcastReceiver or you can override onReceive method like below:

public void onReceive(Context context, Intent intent) {
    AndroidInjection.inject(this, context);
    // your code should be here ...
}

dagger documents: https://dagger.dev/api/2.24/dagger/android/DaggerBroadcastReceiver

Broadcasts overview, So in this case, the default value is " false ". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to  Questions: I am trying to use Dagger2 for DI, it works perfectly fine for Activity/Fragment related classes where there is a onCreate lifecycle event. Now I have a plain Java class which I want to be injected.


<receiver>, How to inject dependencies into Android Components constructors with Dagger 2 But we know that Activities , BroadcastReceivers , Services  The needle goes into your muscle. The barrel holds the medicine and has markings on it like a ruler. The markings are in milliliters (mL). The plunger is used to get medicine into and out of the syringe. Where can I give an intramuscular injection? Thigh: Look at your thigh and divide it into 3 equal parts. The middle third is where the


Injection into Android Component's Constructors is real, Afterward, the launcher Activity is instantiated and its onCreate method is called. The same flow takes place for BroadcastReceiver in the  How to Inject Into a Vein. Injecting medication into a vein can be difficult, but there are some simple strategies that can help you to do it properly. Don't attempt to give an injection unless you have been trained to do so.


Understanding Dependency Injection in Android, It handles things like Service and BroadCastReceiver to make your life easier in the future. 6. Time for connecting activity with this @Module. Dependency Injection, or DI in short, is a design pattern that allows to delegate the creation of objects and their dependencies to another object or framework. It is gaining a lot of interest in…