Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

How to Check if the Android Device is in Dock State?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A docking station is a device that is competent in communicating with the Android kernel to fire docking-related events and revise the docking file state. A docking station can make the system and apps do anything that is programmed. One example is showing a different layout on the docked state. It may also open a music player app, play music automatically on desk mode if it is programmed as such, open a map/navigation app in car mode, etc. Dock Mode is different on different phones, but it often turns your phone into a desk clock, photo slideshow viewer, or music player. You can also set it as a speakerphone when you receive calls. The dock is built into self-amplified speakers or music boxes, or it is a stand-alone unit that connects via USB to a computer, charger, or home theater equipment. Some docks use USB for charging and Bluetooth for playing music. This mode is a feature that can be detected on some phones, including many of the Samsung phones, but is not a feature detect on every phone or every version of a phone. For example, Samsung Galaxy S2, S3, and S4 have a dock mode, but the S5 does not. Please check your phone features to make sure your phone is equipped with dock mode. So in this article let’s discuss how to check if the Android device is in dock state or not or anything else. Note that we are going to implement this project using the Kotlin language. 

Steps to Check if the Android Device is in Dock State

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Go to the activity_main.xml file, which represents the UI of the application. Create a Button, which on click provides the docking state of the device. Below is the code for the activity_main.xml file.


<?xml version="1.0" encoding="utf-8"?>
    <!--Button to check the Dock state-->
        android:text="check" />

Step 3: Working with the MainActivity.kt file

Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.


import android.content.Intent
import android.content.Intent.EXTRA_DOCK_STATE
import android.content.IntentFilter
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Button which onclick gives dock info in the form of a Toast
        val btn = findViewById<Button>(
        btn.setOnClickListener {
            // 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 simply call
            // registerReceiver(), passing in null as the broadcast receiver.
            val dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT)
                    .let { ifilter ->
                        applicationContext.registerReceiver(null, ifilter)
            // dockState is an Integer value received when intent is passed
            val dockState: Int = dockStatus?.getIntExtra(EXTRA_DOCK_STATE, -1) ?: -1
            // isDocked is a Boolean value which if true indicates the device is in dock
            // state and vice-versa
            val isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED
            // Finally depending upon the isDocked value display the Toasts
            if (isDocked) {
                Toast.makeText(applicationContext, "Docked", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(applicationContext, "Not Docked", Toast.LENGTH_SHORT).show()


Depending upon the state, the application shows Toast messages, “Docked” when the device is docked, or “Not Docked” otherwise. The output is not available but this is the standard version of extracting the dock state in Android. Note that Android devices can be docked into several kinds of docks. These include car or home docks and digital versus analog docks. The dock-state is typically closely linked to the charging state as many docks provide power to docked devices.

My Personal Notes arrow_drop_up
Last Updated : 23 Feb, 2021
Like Article
Save Article
Similar Reads