底座的状态跟充电状态类似,很多底座提供充电功能(座充).
底座状态同样使用sticky Intent广播。可以查询设备是否插入底座,哪种底座。
判断当前底座状态
广播的Action是ACTION_DOCK_EVENT,sticky Intent不需要注册真实的接收器
IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);
Intent dockStatus = context.registerReceiver(null, ifilter);
//You can extract the current docking status from the EXTRA_DOCK_STATE extra:
//int dockState = battery.getIntExtra(EXTRA_DOCK_STATE, -1);
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
判断插入底座类型
插入底座有四种类型:
桌面
Low-End (Analog) Desk(猜是显示模拟时钟)
High-End (Digital) Desk(猜是显示数字时钟)
后面两项是在API11以后引入的,所以,后面三者都判断是最好的。
boolean isCar = dockState == EXTRA_DOCK_STATE_CAR;
boolean isDesk = dockState == EXTRA_DOCK_STATE_DESK ||
dockState == EXTRA_DOCK_STATE_LE_DESK ||
dockState == EXTRA_DOCK_STATE_HE_DESK;
监控充电
ACTION_DOCK_EVENT会在插入、拨出底座时广播。所以接收这个广告就可以了
可以用上一步相同的方法读取插入底座的类型和状态.