代码流程分析二:Settings-蓝牙分析-switch开关分析

来源:互联网 发布:c语言选择语句 编辑:程序博客网 时间:2024/05/01 10:31

一:流程图已经分析

点击switch组件打开的流程图:


点击switch组件关闭的流程图:


二:代码的分析:

switch开关:


(1)点击开关:会出现开关on->off,off->on变化;(2)界面会出现:蓝牙正在打开,蓝牙打开,蓝牙正在关闭,蓝牙关闭。

准备工作:

switch点击事件就是:点了开关可以显示不同的界面。
经过测试没有看到bluetoothEnable来控制啊,会看到走了onBluetoothStateChanged这个方法,而这个方法是BluetoothSettings继承的这个父类的DeviceListPreferenceFragment的,而且是这个父类实现了BluetoothCallback这个接口也就是说实际是这个接口中的方法。为什么点击switch开关走的是这样的流程。

准备过程:类:BluetoothSettings中onCreateView()中的onActivityCreated()创建了switch,同时mBluetoothEnabler = new BluetoothEnabler(activity, actionBarSwitch);------------------------>BluetoothEnabler中构造方法中LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);和mLocalAdapter = manager.getBluetoothAdapter();一个获得manager,一个获得mLocalAdapter目的全是为了获得LocalBluetoothAdapter也就是相当于获得BluetoothAdapter

终于发现原来switch的点击事件在BluetoothEnabler中的onCheckedChanged这个方法,因为实现了CompoundButton.OnCheckedChangeListener这个接口


后台流程:点控件就会有BluetoothAdapter的四种状态值,根据状态值做不同的操作----后续分析为什么点了开关,adapter状态值就会变。?
-
点击过程:onBluetoothStateChanged()------------------------------------------>父类DeviceListPreferenceFragment的onBluetoothStateChanged()------------------------->

情形一:第一次上来蓝牙关闭Settings界面:

------------------>BluetoothEnabler构造方法----->LocalBluetoothManager获得实例------>localbluetoothManager构造----->bluetootheventManager就是个广播接收者,给她注册一下,让它时刻准备着--->更新界面,获得状态值10

情形二:第一次上来蓝牙开着Settings界面:
------------------>BluetoothEnabler构造方法----->LocalBluetoothManager获得实例------>localbluetoothManager构造----->bluetootheventManager就是个广播接收者,给她注册一下,让它时刻准备着--->更新界面,获得状态值12

情形一_一:进去蓝牙界面:
--------------------->获得localbluetoothManager的实例-------->bluetoothSettings中进行一系列的实例化switch组件---->构造bluetoothEnable获得localbluetoothManager实例-------->一直查询adapter的状态(LocalBluetoothAdapter中getBluetoothState)

情形二_二:进去蓝牙界面:
跟上面一样,区别就是会多获得一次localbluetoothManager实例,在最后一步查询状态的情形下。


情形三:进去点击switch关闭:

正在关闭:

-------------------------->BluetoothEnabler中的onCheckedChanged---->调mLocalAdapter.setBluetoothEnabled(isChecked);
-------------------------->LocalBluetoothAdapter这个类中的setBluetoothEnabled(),判断是mAdapter.enable()走----->setBluetoothStateInt()设置状态是13一会继续分析分析(mState因为它把adapter的状态设置为13)。突然走,为啥突然走,因为状态设置为13了。
------------------------------>BluetoothEventManager中的AdapterStateChangedHandler(实际就是个广播接收者)接收到了adapter改变状态的通知------>AdapterStateChangedHandler(消息接收者),------>LocalBluetoothAdapter中的setBluetoothStateInt重设状态为13,因为调用了一个接口中的函数所以走--->callback.onBluetoothStateChanged(state);子类方法-->父类onBluetoothStateChanged(state)方法--->设完状态后执行mDeviceManager.onBluetoothStateChanged(state);相当于走这个CachedBluetoothDeviceManager类中的onBluetoothStateChanged方法。在这个方法里如果是正在关闭:走一些关闭的方法。------->BluetoothEnabler中最下面还有一个方法syncBluetoothState。---->获得localbluetoothManager这个实例,然后走syncBluetoothState这个方法的时候发现状态值变为10了。

关闭:
------------------------->BluetoothEventManager中的AdapterStateChangedHandler(实际就是个广播接收者)接收到了adapter改变状态的通知------>AdapterStateChangedHandler(消息接收者)接到了状态是10,给它设状态是10------------>LocalBluetoothAdapter中的setBluetoothStateInt,因为调用了一个接口中的函数所以走--->callback.onBluetoothStateChanged(state);子类方法-->父类onBluetoothStateChanged(state)方法里面有updateProgressUi(false);--->设完状态后执行mDeviceManager.onBluetoothStateChanged(state);相当于走这个CachedBluetoothDeviceManager类中的onBluetoothStateChanged方法。------->BluetoothEnabler中最下面还有一个方法syncBluetoothState。也没发现状态值变。然后走g获得localBletoothManager实例--------->会再看状态值有没有变,------------>然后回到bluetoothSettings中的update更新信息方法,每次进来都要更新界面,getBluetoothState方法就是判断现在adapter的状态值的。


情形四:进去后现在是关闭的,点击switch打开:
正在打开:
-------------------------->BluetoothEnabler中的onCheckedChanged---->调mLocalAdapter.setBluetoothEnabled(isChecked);
-------------------------->LocalBluetoothAdapter这个类中的setBluetoothEnabled(),判断是mAdapter.enable()走----->setBluetoothStateInt()设置状态是11一会继续分析分析(mState因为它把adapter的状态设置为11)。突然走,为啥突然走,因为状态设置为11了。
------------------------------>BluetoothEventManager中的AdapterStateChangedHandler(实际就是个广播接收者)接收到了adapter改变状态的通知------>AdapterStateChangedHandler(消息接收者),------>LocalBluetoothAdapter中的setBluetoothStateInt重设状态为11,因为调用了一个接口中的函数所以走--->callback.onBluetoothStateChanged(state);子类方法-->父类onBluetoothStateChanged(state)方法--->设完状态后执行mDeviceManager.onBluetoothStateChanged(state);相当于走这个CachedBluetoothDeviceManager类中的onBluetoothStateChanged方法。------->BluetoothEnabler中最下面还有一个方法syncBluetoothState。走这个方法的时候发现状态值变为12了。


打开:
------------------------->BluetoothEventManager中的AdapterStateChangedHandler(实际就是个广播接收者)接收到了adapter改变状态的通知------>AdapterStateChangedHandler(消息接收者)接到了状态是12,给它设状态是12------------>LocalBluetoothAdapter中的setBluetoothStateInt,因为调用了一个接口中的函数所以走--->callback.onBluetoothStateChanged(state);子类方法-->父类onBluetoothStateChanged(state)方法--->设完状态后执行mDeviceManager.onBluetoothStateChanged(state);相当于走这个CachedBluetoothDeviceManager类中的onBluetoothStateChanged方法。------->BluetoothEnabler中最下面还有一个方法syncBluetoothState。也没发现状态值变。然后走g获得localBletoothManager实例--------->会再看状态值有没有变,------------>然后回到bluetoothSettings中的update更新信息方法,每次进来都要更新界面,getBluetoothState方法就是判断现在adapter的状态值的。

前台流程:
因为点了开关,会在后台设置状态值。前台会判断蓝牙状态值来改变界面内容。
  • 执行updateContent()这个方法:会显示4种状态,蓝牙开着,蓝牙正在打开,蓝牙关闭,蓝牙正在关闭。
  • 蓝牙开着:
  • 蓝牙正在打开:BluetoothAdapter.STATE_TURNING_ON设置内容
  • 蓝牙正在关闭:BluetoothAdapter.STATE_TURNING_OFF:设置内容
  • 蓝牙关闭:BluetoothAdapter.STATE_OFF设置内容

0 0