Whenever a device is docked or undocked, the ACTION_DOCK_EVENT action is broadcast. To track changes in the status of the device dock, simply register the broadcast receiver in the application manifest, as shown in the following snippet below:
<action android:name="android.intent.action.ACTION_DOCK_EVENT"/>
If the device is connected, it can be docked in any of the four types of dock:
- Car
- Desktop
- Low Screen (Analog) Table
- High quality (digital) table
Dock status details are included as optional in the sticky translation of ACTION_DOCK_EVENT. Since it is sticky, you do not need to register BroadcastReceiver. You can simply call registerReceiver (), passing zero as a broadcast receiver, as shown in the following snippet.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT); Intent dockStatus = context.registerReceiver(null, ifilter);
You can extract the current docking status from EXTRA_DOCK_STATE additionally:
int dockState = battery.getIntExtra(EXTRA_DOCK_STATE, -1); boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
The status of the dock can be found at
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;
EDIT:
If your application still does not receive broadcast, try this code to send a manual broadcast and check the code:
adb shell am broadcast -a android.intent.action.POWER_CONNECTED -n com.jm.monitoringbatterydemo/.PowerConnectionReceiver
Change the name of the broadcast and your receiver.