[9] use of the injection framework RoboGuice: (Your First Injected Service and BroadcastReceiver), roboguiceinjected
In the previous article, we briefly introduced the use of RoboGuice ([8] injection framework RoboGuice: (Your First Injected Fragment). Let's take a look at the Service) and BroadCast Receiver Injection
(1): Like Robo * Activities, RoboServices and RoboIntentServices automatically accept injection through RoboGuice.
The following is an example of android service injected with RoboGuice:
public class MyService extends RoboService { @Inject ComputeFooModule computeFooModule; public void onCreate() { super.onCreate(); //All injections are available from here : computeFooModule.setUp(); } public int onStartCommand(Intent intent, int flags, int startId) { computeFooModule.computeFoo(); return super.onStartCommand(); }}
RoboGuice also provides the RoboIntentService as the injection base class for IntentService.
(2) broadcast receiver Injection
With RoboGuice, android BroadCast Receiver can also accept injection.
public class MyBroadcastReceiver extends BroadcastReceiver { @Inject ComputeFooModule computeFooModule; protected void handleReceive(Context context, Intent intent) { //All injections are available from here : computeFooModule.setUp().computeFoo(); ... }}
(3) [note] In RoboGuice, all broadcast recipients can accept injection throughout the application. This means that even ContextSingleton will be converted to Singleton here.