Determine and monitor the docking state and type
Stay organized with collections
Save and categorize content based on your preferences.
Android-powered devices can be docked into different kinds of docks. These
include car docks, home docks, and digital or analog docks. The dock state is
typically closely linked to the charging state, as many docks provide power to
docked devices.
Your app affects the phone's update rate in the dock state. You can increase
the update frequency of a sports news app when it's in the desktop dock, or
disable your updates completely if the device is car docked. Conversely, you can
maximize your updates while car docked if your background service is updating
traffic conditions.
The dock state is also broadcast as a sticky
Intent
,
letting you query whether the device is docked and in which kind of dock if
so.
Determine the current docking state
The dock state details are included as an extra in a
sticky broadcast of the
ACTION_DOCK_EVENT
action. Because it's sticky, you can call
registerReceiver()
,
passing in null
as the broadcast receiver. The following snippet
shows how to complete this process:
Kotlin
val dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT).let { ifilter ->
context.registerReceiver(null, ifilter)
}
Java
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:
Kotlin
val dockState: Int = dockStatus?.getIntExtra(EXTRA_DOCK_STATE, -1) ?: -1
val isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED
Java
int dockState -1;
if (dockStatus != null) {
dockState = dockStatus.getIntExtra(EXTRA_DOCK_STATE, -1);
}
boolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;
Determine the current dock type
If a device is docked, it can be docked in any of the following four
different dock types:
- Car
- Desk
- Low-End (Analog) desk
- High-End (Digital) desk
The latter two options are only introduced in Android API level 11, so it's
good practice to check for all three desk types when you are only interested in
the type of dock rather than it being digital or analog specifically:
Kotlin
val isCar: Boolean = dockState == EXTRA_DOCK_STATE_CAR
val isDesk: Boolean = dockState == EXTRA_DOCK_STATE_DESK
|| dockState == EXTRA_DOCK_STATE_LE_DESK
|| dockState == EXTRA_DOCK_STATE_HE_DESK
Java
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;
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-01-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-01-03 UTC."],[],[],null,["# Determine and monitor the docking state and type\n\nAndroid-powered devices can be docked into different kinds of docks. These\ninclude car docks, home docks, and digital or analog docks. The dock state is\ntypically closely linked to the charging state, as many docks provide power to\ndocked devices.\n\nYour app affects the phone's update rate in the dock state. You can increase\nthe update frequency of a sports news app when it's in the desktop dock, or\ndisable your updates completely if the device is car docked. Conversely, you can\nmaximize your updates while car docked if your background service is updating\ntraffic conditions.\n\nThe dock state is also broadcast as a sticky\n[Intent](/reference/android/content/Intent),\nletting you query whether the device is docked and in which kind of dock if\nso.\n\nDetermine the current docking state\n-----------------------------------\n\nThe dock state details are included as an extra in a\n[sticky broadcast](/topic/security/risks/sticky-broadcast) of the\n[ACTION_DOCK_EVENT](/reference/android/content/Intent#ACTION_DOCK_EVENT)\naction. Because it's sticky, you can call\n[registerReceiver()](/reference/android/content/Context#registerReceiver(android.content.BroadcastReceiver, android.content.IntentFilter)),\npassing in `null` as the broadcast receiver. The following snippet\nshows how to complete this process: \n\n### Kotlin\n\n```kotlin\nval dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT).let { ifilter -\u003e\n context.registerReceiver(null, ifilter)\n}\n```\n\n### Java\n\n```java\nIntentFilter ifilter = new IntentFilter(Intent.ACTION_DOCK_EVENT);\nIntent dockStatus = context.registerReceiver(null, ifilter);\n```\n\nYou can extract the current docking status from the\n`EXTRA_DOCK_STATE` extra:\n\n\n### Kotlin\n\n```kotlin\nval dockState: Int = dockStatus?.getIntExtra(EXTRA_DOCK_STATE, -1) ?: -1\nval isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED\n```\n\n### Java\n\n```java\nint dockState -1;\nif (dockStatus != null) {\n dockState = dockStatus.getIntExtra(EXTRA_DOCK_STATE, -1);\n}\nboolean isDocked = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED;\n```\n\nDetermine the current dock type\n-------------------------------\n\nIf a device is docked, it can be docked in any of the following four\ndifferent dock types:\n\n- Car\n- Desk\n- Low-End (Analog) desk\n- High-End (Digital) desk\n\nThe latter two options are only introduced in Android API level 11, so it's\ngood practice to check for all three desk types when you are only interested in\nthe type of dock rather than it being digital or analog specifically: \n\n### Kotlin\n\n```kotlin\nval isCar: Boolean = dockState == EXTRA_DOCK_STATE_CAR\nval isDesk: Boolean = dockState == EXTRA_DOCK_STATE_DESK\n || dockState == EXTRA_DOCK_STATE_LE_DESK\n || dockState == EXTRA_DOCK_STATE_HE_DESK\n```\n\n### Java\n\n```java\nboolean isCar = dockState == EXTRA_DOCK_STATE_CAR;\nboolean isDesk = dockState == EXTRA_DOCK_STATE_DESK ||\n dockState == EXTRA_DOCK_STATE_LE_DESK ||\n dockState == EXTRA_DOCK_STATE_HE_DESK;\n```"]]