zoukankan      html  css  js  c++  java
  • 【九】注入框架RoboGuice使用:(Your First Injected Service and BroadcastReceiver)

            上一篇我们简单的介绍了一下RoboGuice的使用(【八】注入框架RoboGuice使用:(Your First Injected Fragment)),今天我们来看下服务(Service)和广播接受者(BroadCast Receiver)的注入

           (一):和Robo*Activities一样,RoboServices和RoboIntentServices通过RoboGuice也自己主动接受注入。

           以下是一个使用RoboGuice注入的android service的样例:

    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也为IntentService提供了注入基类,RoboIntentService。

         (二):广播接收者注入

          通过RoboGuice ,android BroadCast Receiver也能接受注入。

    public class MyBroadcastReceiver extends BroadcastReceiver {
    
       @Inject ComputeFooModule computeFooModule;
    
        protected void handleReceive(Context context, Intent intent) {
            //All injections are available from here :
            computeFooModule.setUp().computeFoo();
            ...
       }
    
    }
        (三):【注意】在RoboGuice中,全部广播接受者能够在整个应用程序范围内接受注入,这就是即使是ContextSingleton在这里也会转成Singleton。

      

  • 相关阅读:
    scrapy(二)内容获取
    scrapy(一)建立一个scrapy项目
    scrapy(四)使用redis
    scrapy(三)使用mongoDB
    索引处的解码字符串
    Golang竞争状态
    Golang之泛型编程-细节
    区块链学这个就够了-DLT(一)
    Linux日志分析-Ubuntu(一)
    经典博弈-int
  • 原文地址:https://www.cnblogs.com/blfshiye/p/5147959.html
Copyright © 2011-2022 走看看