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:
- Car
- Desk
- Low-End (Analog) Desk
- 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:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.
Similar Reads
Determine Current Dock Type using Android Jetpack Compose An android device is docked when it is connected to a charging cable or connected to a car charging. Dock state of an android device is related to charging. In this article, we will be building a simple application in which we will take a look at How to determine the current dock type in android usi
5 min read
How to Detect Text Type Automatically in Android? In this article, we are going to implement a feature related to TextView which is very important in every perspective. While using any social Media App or Notes app you may have seen that when we type something, it automatically detect the text type like email, phone, or a URL. Here we are going to
2 min read
How to Check if the Android Device is in Dock State? 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
4 min read
How to Get the File Extension in Android? File extensions are strings of characters that follow the last dot (.) in a file name and indicate the type of the file. They are used by the operating system to determine how to handle the file, what application to use to open it, and what icon to display for the file. Let's see the implementation:
3 min read
How to Detect Cellular Network Type (2G, 3G, 4G and 5G) in Android? Android devices like mobiles and tablets are available with SIM card slots. Users can use them to connect to a particular network. Over time, telecommunication networks have changed rapidly giving users the extent of a new band with higher data speed. Devices are manufactured according to the advanc
4 min read
How to Get Current Default Language of Android Device Programmatically? Smartphones seem to be predicting and showing us results from what we think in our minds. If you think of traveling, you shall soon expect something related to it in your device's feed. This is the next level of technology, where data helps in developing businesses. The software collects personal da
2 min read