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
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)
val dockStatus: Intent? = IntentFilter(Intent.ACTION_DOCK_EVENT).let { ifilter ->
applicationContext.registerReceiver( null , ifilter)
}
val dockState: Int = dockStatus?.getIntExtra(Intent.EXTRA_DOCK_STATE, - 1 ) ?: - 1
val isDocked: Boolean = dockState != Intent.EXTRA_DOCK_STATE_UNDOCKED
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
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.
Share your thoughts in the comments
Please Login to comment...