Android @Inject

背景描述

Android大版本升级,在AndroidS的源码中发现好多模块都有@Inject。当前在学习systemui模块,发现好多类找不到new的地方,没有初始化。----一脸懵逼

咋办捏?

我想要在systemui中添加一个功能,监听广播弹出一个dialog,然后打开蓝牙开关。
systemui中蓝牙控制的接口BluetoothController,实现类BluetoothControllerImpl

//打开蓝牙
bluetoothController.setBluetoothEnabled(true);

但是全局没有new 这两个类的地方,我自定义一个类,不知道如何初始化蓝牙控制器。
人世间最凄惨的事情莫过于此,明没有思路,有想法,却不知道咋办。
这两个类的构造方法都是@Inject,其它使用蓝牙控制器的类,也差不多是这样的,直接在构造方法中声明入参,然后就直接在类里使用了,完全没看到创建对象的地方。—杯具啊

思路1—借鸡下蛋

在其它有BluetoothController对象的类里:
声明dialog变量—new 对象—入参传入需要的参数即可。

//自定义类
public MyDialogControllerImpl(Context context,BluetoothController bluetoothController,BroadcastDispatcher broadcastDispatcher) {
        this.mContext = context;
        this.mDataUtils = new DataUtils(context);
        this.bluetoothController = bluetoothController;
        this.mBroadcastDispatcher = broadcastDispatcher;
        init();
    }
思路2—照猫画虎
//自定义类
@Inject
public MyDialogControllerImpl(Context context,BluetoothController bluetoothController,BroadcastDispatcher broadcastDispatcher) {
        this.mContext = context;
        this.mDataUtils = new DataUtils(context);
        this.bluetoothController = bluetoothController;
        this.mBroadcastDispatcher = broadcastDispatcher;
        init();
    }

然后再SystemUI/src/com/android/systemui/statusbar/phone/dagger/StatusBarPhoneModule.java

@Provides
@SysUISingleton
static StatusBar provideStatusBar(
            Context context,
            ...
            MyDialogControllerImpl myDialogControllerImpl,//添加这一行
            ...
叮~

就可以了。哇偶,好神奇啊。为啥啊,它是怎么知道MyDialogControllerImpl我的类需要构造方法要几个什么类型的对象哇。。。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AndroidInjectAndroid 注解框架,以简化 Android 开发 目前完成的注解(持续增加中): @AINoTitle: 类注解, 只适用于Activity(需继承于AIActivity), 设置Activity不显示Title     @AIFullScreen: 类注解, 只适用于Activity(需继承于AIActivity), 设置Activity全屏     @AILayout: 类注解         value[int]: 用于设置该Activity的布局 ---- setContentView(resId);     @AIView: 属性注解         id[int]: 用于绑定控件 ---- findViewById(resId);(default identifier[R.id.{field name}] if did not set id)         clickMethod[String]: 用于设置控件点击事件的回调方法, 可选, 方法名称任意, 参数必须为(View view)         longClickMethod[String]: 用于设置控件长按的回调方法, 可选, 方法名任意, 参数必须为(View view)         itemClickMethod[String]: 用于设置控件item点击的回调方法, 可选, 方法名任意, 参数必须为(AdapterView, View, int, long)         itemLongClickMethod[String]: 用于设置控件item长按的回调方法, 可选, 方法名任意, 参数必须为(AdapterView, View, int, long)     @AIBean: 属性注解, 为该属性生成一个对象并注入, 该对象必须有个默认的不带参数的构造方法     @AISystemService: 属性注解,为该属性注入系统服务对象     @AIClick: 方法注解         value[int[], 所要绑定控件的id]: 用于绑定控件点击事件的回调方法, 方法名称任意, 参数必须为(View view)     @AIItemClick: 方法注解         value[int[], 所要绑定控件的id]: 用于绑定控件item点击事件的回调方法, 方法名称任意, 参数必须为(AdapterView, View, int, long)     @AILongClick: 方法注解         value[int[], 所要绑定控件的id]: 用于绑定控件长按事件的回调方法, 方法名称任意, 参数必须为(View view)     @AIItemLongClick: 方法注解         value[int[], 所要绑定控件的id]: 用于绑定控件item长按事件的回调方法, 方法名称任意, 参数必须为(AdapterView, View, int, long)     @AIScreenSize: 属性注解         用于注入当前设备的屏幕大小(宽高)     @AIGet: 方法注解         value[String, 所要请求的url]:表示以GET来请求url         connTimeout[int, 连接超时时间]:连接一个url的连接等待时间         soTimeout[int, response返回超时时间]:连接上一个url,获取response的返回等待时间     @AIPost: 方法注解         value[String, 所要请求的url]:表示以Post来请求url         connTimeout[int, 连接超时时间]:连接一个url的连接等待时间         soTimeout[int, response返回超时时间]:连接上一个url,获取response的返回等待时间     @AIParam: 方法参数注解         value[String, 请求的参数别名]:注入@AIGet或@AIPost注解方法的请求参数     @AINetWorker: 属性注解         注入网络请求服务     @AIUpload: 方法注解         value[String, 所要请求的url]:表示要上传的url,默认用post请求(不需要使用@AIPost注解)         connTimeout[int, 连接超时时间]:连接一个url的连接等待时间         soTimeout[int, response返回超时时间]:连接上一个url,获取response的返回等待时间         注意:使用此注解的方法参数需要包含Collection或其子类型集合 或者包含File对象 来作为要上传的文件 使用fragment的注解,需要android-support-v4.jar的支持(以兼容低版本) 使用网络请求的注解,需要gson.jar的支持 使用文件上传的注解,需要httpmime.jar的支持

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值