How to Get the Device Information in Android?
In the present world, sometimes we want to access the device information of the user to expand our business e.g. An e-commerce company may want to access the mobile’s brand of the user to provide (more specifically, to target the user for high/low price’s items) recommendation. We can find the device (hardware and software) information using Build Package in Android.
Step by Step Implementation
To get the device information follow these steps. We are going to implement it using the Kotlin language.
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
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.
XML
<? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:gravity = "center" android:orientation = "vertical" android:padding = "20dp" tools:context = ".MainActivity" > < TextView android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:text = "Your Device Info" android:textSize = "18sp" android:textStyle = "bold" /> < TextView android:id = "@+id/tv_displayInfo" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginTop = "15dp" android:textSize = "16sp" /> </ LinearLayout > |
Step 3: Working with the MainActivity.java 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.annotation.SuppressLint import android.os.Build import android.os.Bundle import android.provider.Settings import android.widget.TextView import androidx.appcompat.app.AppCompatActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super .onCreate(savedInstanceState) setContentView(R.layout.activity_main) val tvDisplay: TextView = findViewById(R.id.tv_displayInfo) tvDisplay.text = getSystemDetail() } @SuppressLint ( "HardwareIds" ) private fun getSystemDetail(): String { return "Brand: ${Build.BRAND} \n" + "DeviceID: ${ Settings.Secure.getString( contentResolver, Settings.Secure.ANDROID_ID ) } \n" + "Model: ${Build.MODEL} \n" + "ID: ${Build.ID} \n" + "SDK: ${Build.VERSION.SDK_INT} \n" + "Manufacture: ${Build.MANUFACTURER} \n" + "Brand: ${Build.BRAND} \n" + "User: ${Build.USER} \n" + "Type: ${Build.TYPE} \n" + "Base: ${Build.VERSION_CODES.BASE} \n" + "Incremental: ${Build.VERSION.INCREMENTAL} \n" + "Board: ${Build.BOARD} \n" + "Host: ${Build.HOST} \n" + "FingerPrint: ${Build.FINGERPRINT} \n" + "Version Code: ${Build.VERSION.RELEASE}" } } |
Output: