Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--Button to check the Dock state-->
    <Button
        android:id="@+id/btnCheck"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="check" />
  
</RelativeLayout>


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.

Kotlin




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
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Button which onclick gives dock info in the form of a Toast
        val btn = findViewById<Button>(R.id.btnCheck)
        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()
            }
        }
    }
}


Output

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.



Last Updated : 23 Feb, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads