前言
有些时候,我们需要在某些Activity中监听某些状态。当然不可能在每个Activity中创建一个实例去查询和绑定吧。这时候,就需要一个统一管理状态的框架,下面就以蓝牙状态监听为例,描述如何开发一个监听框架。
文件架构
代码
监听者注解
创建文件 RDBluetoothObserver ,注解类,用于描述监听者的方法
/**
* 蓝牙状态观察者注解
*
* @author D10NG
* @date on 2019-11-12 09:25
*/
@kotlin.annotation.Target(AnnotationTarget.FUNCTION)
@kotlin.annotation.Retention(AnnotationRetention.RUNTIME)
annotation class RDBluetoothObserver
状态值枚举类
创建文件 RDBluetoothStatus
/**
* 蓝牙开关状态
*
* @author D10NG
* @date on 2019-11-12 09:27
*/
enum class RDBluetoothStatus(val msg: String) {
NONE(msg = "当前设备不支持蓝牙BLE"),
STATE_TURNING_ON(msg = "正在打开蓝牙"),
STATE_ON(msg = "蓝牙已打开"),
STATE_TURNING_OFF(msg = "正在关闭蓝牙"),
STATE_OFF(msg = "蓝牙已关闭"),
CONNECTED(msg = "蓝牙已连接"),
DISCONNECTED(msg = "蓝牙已断开连接")
}
编写接口
创建文件 IBluetoothCallBack
/**
* 蓝牙状态接口
*
* @author D10NG
* @date on 2019-11-12 09:30
*/
interface IBluetoothCallBack {
/**
* 注册
* @param obj
*/
fun register(obj: Any)
/**
* 取消注册
* @param obj
*/
fun unRegister(obj: Any)
/**
* 取消注册
*/
fun unRegisterAll()
/**
* 打开蓝牙
*/
fun openBluetooth()
/**
* 关闭蓝牙
*/
fun closeBluetooth()
/**
* 获取蓝牙状态
* @return
*/
fun getBluetoothStatus() : RDBluetoothStatus
}
业务逻辑
创建文件 BluetoothCallBack
/**
* 蓝牙状态监听
*
* @author D10NG
* @date on 2019-11-12 09:31
*/
class BluetoothCallBack constructor(
application: Application
) : IBluetoothCallBack {
// 观察者,key=类、value=方法
private val checkManMap = HashMap<Any, Method>()
@Volatile
private var bluetoothStatus: RDBluetoothStatus
private val bluetoothReceiver = BluetoothReceiver()
init {
// 初始化广播
val intentFilter = IntentFilter()
// 监视蓝牙关闭和打开的状态
intentFilter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED)
intentFilter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
intentFilter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
// 开始监听
application.registerReceiver(bluetoothReceiver, intentFilter)
// 初始化蓝牙状态
val bleManager = application.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
val adapter = bleManager.adapter
bluetoothStatus = when {
adapter == null -> RDBluetoothStatus.NONE
adapter.isEnabled -> RDBluetoothStatus.STATE_ON
else -> RDBluetoothStatus.STATE_OFF
}
}
override fun register(obj: Any) {
val clz = obj.javaClass
if (!checkManMap.containsKey(clz)) {
val method = AnnotationUtils.findAnnotationMethod(
clz, RDBluetoothObserver::class.java,
RDBluetoothStatus::class.java) ?: return
checkManMap[obj] = method
}
}
override fun unRegister(obj: Any) {
checkManMap.remove(obj)
}
override fun unRegisterAll() {
checkManMap.clear()
}
override fun openBluetooth() {
val ble = BluetoothAdapter.getDefaultAdapter()
ble?.enable()
}
override fun closeBluetooth() {
val ble = BluetoothAdapter.getDefaultAdapter()
ble?.disable()
}
override fun getBluetoothStatus(): RDBluetoothStatus = bluetoothStatus
// 执行
private fun post(status: RDBluetoothStatus) {
bluetoothStatus = status
val set: Set<Any> = checkManMap.keys
for (obj in set) {
val method = checkManMap[obj] ?: continue
method.invoke(obj, status)
}
}
inner class BluetoothReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action ?: return
when (action) {
BluetoothAdapter.ACTION_STATE_CHANGED -> {
when(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
BluetoothAdapter.STATE_TURNING_ON -> post(RDBluetoothStatus.STATE_TURNING_ON)
BluetoothAdapter.STATE_ON -> post(RDBluetoothStatus.STATE_ON)
BluetoothAdapter.STATE_TURNING_OFF -> post(RDBluetoothStatus.STATE_TURNING_OFF)
BluetoothAdapter.STATE_OFF -> post(RDBluetoothStatus.STATE_OFF)
}
}
BluetoothDevice.ACTION_ACL_CONNECTED -> {
post(RDBluetoothStatus.CONNECTED)
}
BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
post(RDBluetoothStatus.DISCONNECTED)
}
}
}
}
}
暴露接口 单例
创建文件 RDBluetoothManager
/**
* 蓝牙管理器
*
* @author D10NG
* @date on 2019-11-12 09:24
*/
class RDBluetoothManager constructor(
application: Application
) : IBluetoothCallBack {
private val bluetoothCallBack : IBluetoothCallBack
companion object {
@Volatile
private var INSTANCE : RDBluetoothManager? = null
@JvmStatic
fun getInstance(application: Application) : RDBluetoothManager {
val temp = INSTANCE
if (null != temp) {
return temp
}
synchronized(this) {
val instance = RDBluetoothManager(application)
INSTANCE = instance
return instance
}
}
}
init {
bluetoothCallBack = BluetoothCallBack(application)
}
override fun register(obj: Any) {
bluetoothCallBack.register(obj)
}
override fun unRegister(obj: Any) {
bluetoothCallBack.unRegister(obj)
}
override fun unRegisterAll() {
bluetoothCallBack.unRegisterAll()
}
override fun openBluetooth() {
bluetoothCallBack.openBluetooth()
}
override fun closeBluetooth() {
bluetoothCallBack.closeBluetooth()
}
override fun getBluetoothStatus(): RDBluetoothStatus = bluetoothCallBack.getBluetoothStatus()
}
混淆封装
如果你想打包成第三方库的话,看我的另一篇文章:
Android 简单开发sdk教程一 接口写法和混淆规则