Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Determine the Current Dock Type in Android?

  • Last Updated : 23 Feb, 2021

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

An Android Device is said to be DOCKED when it is connected to a power supply or a metered exchange system involving the charging and the exchange of multimedia. Dock State is very closely related to the Charging State of the device. Dock State gives insights regarding the charging or the connection type. Android devices are capable of docking into several kinds of docks. For example, when an Android device is connected to a music system such as a Home Music System over a compatible USB cable, it is said to be DOCKED in a High-End Desk. Similarly, when an Android device is connected to a car charger, it is DOCKED in a Car. So basically, there are four different types of dock:

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!
  1. Car
  2. Desk
  3. Low-End (Analog) Desk
  4. High-End (Digital) Desk

Note: The Low-End (Analog) Desk and the High-End (Digital) Desk were introduced to Android in API level 11.

  • Car Dock: A Car Dock is a docking center, that lets the device charge, and transfers media when connected. 
  • Desk Dock: A Desk Dock is a docking station, which is an interface device allowing portable computers with little or no effort to attach to other devices. Docking stations enable mobile devices to convert it to a desktop computer at the office or home.
  • Low-End Desk: These docks are operated at a lower current. Ex: Low Output Power Banks (<1 Ampere)
  • High-End Desk: These docks are operated at a higher current. Ex: Fast Chargers, High Output Power Banks (2.1 Amperes)

So in this article let’s determine the current dock type in Android. Note that we are going to implement this project using the Kotlin language. 

Approach

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

There is nothing to do with the activity_main.xml file. So keep the file as it is.

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. 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.IntentFilter
import android.os.Bundle
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)
          
        // 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(Intent.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
  
        // isCar and isDesk are a Boolean values, 
        // either of which if true indicates
        // the device is in a particular dock
        val isCar: Boolean = dockState == Intent.EXTRA_DOCK_STATE_CAR
        val isDesk: Boolean = dockState == Intent.EXTRA_DOCK_STATE_DESK
                || dockState == Intent.EXTRA_DOCK_STATE_LE_DESK
                || dockState == Intent.EXTRA_DOCK_STATE_HE_DESK
  
        // Output Cases
        when {
            isCar -> Toast.makeText(applicationContext, "Dock CAR", Toast.LENGTH_SHORT).show()
            isDesk -> Toast.makeText(applicationContext, "Dock DESK", Toast.LENGTH_SHORT).show()
            !isDocked -> Toast.makeText(applicationContext, "Not Docked", Toast.LENGTH_SHORT).show()
        }
    }
}

Depending upon the state, the application shows Toast messages, “Dock CAR” when the device is docked in a Car, “Dock DESK” if docked on a desk, or “Not Docked” otherwise. The output is not available but this is the standard version of extracting the dock state in Android. 




My Personal Notes arrow_drop_up

Start Your Coding Journey Now!