How to inject into a BroadcastReceiver

carlosmaciel picture carlosmaciel · Nov 30, 2015 · Viewed 16.1k times · Source

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.

Answer

Chatura Dilan picture Chatura Dilan · Jul 3, 2016

Same as you inject to an Activity

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