How to enter in BroadcastReceiver

Did someone already have to introduce an existing class with some business logic into BroadcastReceiver using a dagger?

I use dagger 1 and already found a good example ( https://github.com/adennie/fb-android-dagger ), but I could not find how we can add an already existing class belonging to another module to BroadcastReceiver.

Any help or advice is appreciated.

+16
source share
6 answers

I managed to introduce usage examples into my translation, defining a module that will provide me with the necessary use cases, and I will add the module to 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; } 
+3
source

The same that you enter in Activity

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

It may be too late to answer this question, but I AppWidgetProvider an AppWidgetProvider example from my recent project in which I tried to implement an AppWidgetProvider which is a direct subclass of BroadcastReceiver .

We need to implement a modified service in BroadcastReceiver :

 @Module public class NetModule { /** shrunk for simplicity 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 the BroadcastReceiver you want @ContributesAndroidInjector :

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

If you forgot to add this module, you will receive an error message:

java.lang.IllegalArgumentException: injector factory not bound to class <>

Then a component with both modules except AndroidInjectionModule

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

Then, in your Application class, you implement the 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 add your BroadcastReceiver inside onReceive () before calling 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 learn more about how to embed documents on Android components.

I built a small sample: broadcast injection .

+17
source

Dagger 2 example for embedding objects in BroadcastReceiver.

BroadcastReceiverModule.kt

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

AppComponent.kt

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

Application class

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

Class BroadcastReceiver

 class MyTestReceiver : BroadcastReceiver() { @Inject lateinit var anInjectedObject: MyInjectObject override fun onReceive(context: Context, intent: Intent) { AndroidInjection.inject(this, context) anInjectedObject.doSomthing() } } 
+8
source

@ s-hunter Thank you man, it really helped me.

0
source

You can use the DaggerBroadcastReceiver or override the onReceive method, as shown below:

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

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

0
source

Source: https://habr.com/ru/post/1237105/


All Articles