Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 425
carlosmaciel Avatar asked Nov 30 '15 14:11

carlosmaciel


People also ask

How do you shoot in BroadcastReceiver?

inject(this, context); in onReceive method. class MyReceiver : DaggerBroadcastReceiver() { @Inject lateinit var repository: MyRepository override fun onReceive(context: Context?, intent: Intent?) { super. onReceive(context, intent) // Do your stuff... } }

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.

What does a BroadcastReceiver do?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Which intent is used by BroadcastReceiver?

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.


1 Answers

Same as you inject to an Activity

public void onReceive(Context context, Intent intent) {         ((Application) context.getApplicationContext()).getInjector().inject(this);     } 
like image 88
Chatura Dilan Avatar answered Sep 18 '22 21:09

Chatura Dilan