Introduction
This code snippet showcases how to programmatically retrieve various details about the Android operating system running on a device using Kotlin. This information can be useful for developers to understand the device's environment and tailor functionalities accordingly.
Breakdown of the Code
The code resides in the MainActivity.kt
file and utilizes the Build
class from the android.os
package. This class provides access to various system properties about the Android build.
Retrieving OS Details:
- The code starts by setting the text of a TextView element within the activity layout to "Device OS Details".
- It then retrieves and displays information like the base OS (
Build.VERSION.BASE_OS
), development codename (Build.VERSION.CODENAME
), internal build version (Build.VERSION.INCREMENTAL
), and user-visible version string (Build.VERSION.RELEASE
).
API Level and Security Patch:
- The code checks if the API level is higher than or equal to Android Marshmallow (API level 23) using
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
. If true, it retrieves the developer preview revision for pre-release SDK (Build.VERSION.PREVIEW_SDK_INT
) and the user-visible security patch level (Build.VERSION.SECURITY_PATCH
).
- The code checks if the API level is higher than or equal to Android Marshmallow (API level 23) using
Advanced Version Information (API level 21 and above):
- The code checks if the API level is higher than or equal to Android R (API level 30) using
Build.VERSION.SDK_INT >= Build.VERSION_CODES.R
. If true, it retrieves the combined string for release version or codename (Build.VERSION.RELEASE_OR_CODENAME
).
- The code checks if the API level is higher than or equal to Android R (API level 30) using
Main Information:
- Finally, the code retrieves and displays the most crucial information, the SDK version (API level) using
Build.VERSION.SDK_INT
. This represents the Android version currently running on the device.
- Finally, the code retrieves and displays the most crucial information, the SDK version (API level) using
Summary
This code snippet demonstrates a practical approach for developers to access and display detailed information about the device's Android operating system programmatically in a Kotlin application. The retrieved information includes various aspects like the base OS, build details, user-visible versions, security patch level, and most importantly, the API level (SDK version). This can be beneficial for tailoring app functionalities based on the device's capabilities or displaying relevant messages to the user based on their Android version.
package com.example.jetpack
import android.os.Build
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.text = "Device OS Details"
// base OS build the product is based on
textView.append("\n\nBase OS: ${Build.VERSION.BASE_OS}")
// current development codename, or the string "REL"
// if this is a release build
textView.append("\nCodeName: ${Build.VERSION.CODENAME}")
// internal value used by the underlying source
// control to represent this build
textView.append("\nIncremental: ${Build.VERSION.INCREMENTAL}")
// developer preview revision of a pre-release sdk
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.append("\nPreview SDK Int: ${Build.VERSION.PREVIEW_SDK_INT}")
}
// user-visible version string.
textView.append("\nRelease: ${Build.VERSION.RELEASE}")
// version string show to the user; may be RELEASE or
// CODENAME if not a final release build
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
textView.append("\nRelease Or CodeName: " +
"${Build.VERSION.RELEASE_OR_CODENAME}")
}
// user-visible security patch level
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
textView.append("\nSecurity Patch: ${Build.VERSION.SECURITY_PATCH}")
}
// sdk version of the software currently running on this hardware device
textView.append("\n\nSDK Int (API Level): ${Build.VERSION.SDK_INT}")
}
}
<?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:id="@+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E5E4E2"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-condensed"
android:textColor="#191970"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
- android kotlin - Switch button listener
- android kotlin - Create ImageView programmatically
- android kotlin - ImageView rounded corners transparent
- android kotlin - ImageView set image from assets
- android kotlin - ImageView border shadow
- android kotlin - ImageView tint programmatically
- android kotlin - ImageView rounded top corners programmatically
- android kotlin - ImageView set image from Uri
- android kotlin - Get battery capacity programmatically
- android kotlin - Get battery temperature programmatically
- android kotlin - Get battery health programmatically
- android kotlin - Repeat a task periodically
- android kotlin - Do a task after a delay
- android kotlin - Get screen size category
- android kotlin - Play default ringtone programmatically