[9] use of the injection framework roboguice: (your first injected service and broadcastreceiver)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.