Android method for judging and listening base state and type
- 2020-06-01 11:00:51
- OfStack
The state of the base is similar to that of the charging state. Many bases provide charging function (base charging).
The base state is also broadcast using sticky Intent. You can query whether the device is inserted into the base and what kind of base.
Determine the current base state
The broadcast Action is ACTION_DOCK_EVENT, and sticky Intent does not need to register a real receiver
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;
Determine the type of insert base
There are 4 types of insert base:
An on-board
desktop
Low-End (Analog) Desk( Guess is to display the analog clock )
High-End (Digital) Desk( Guess is to display a digital clock )
The last two terms were introduced after API11, so the last three were judged to be the best.
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;
Monitor the charging
ACTION_DOCK_EVENT will broadcast when the dock is inserted and dialed. So just accept the AD
<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>
You can read the type and status of the insert base in the same way as in step 1.