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.
[9] use of the injection framework roboguice: (your first injected service and broadcastreceiver)