บทนำ
ตัวอย่างนี้แสดงวิธีดึงเวอร์ชัน Android API (SDK INT) และชื่อรุ่น (Codename) ของอุปกรณ์โดยใช้ Build.VERSION
ขั้นตอนที่ 1: สร้างโปรเจกต์
สร้างโปรเจกต์ Android ใหม่ใน Android Studio
ขั้นตอนที่ 2: Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="4dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Android Version Info"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button"
android:layout_centerInParent="true"
android:layout_marginBottom="12sp"
android:text=""
android:textSize="24sp"
android:textStyle="bold|italic" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Get Android API Version"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.kt
import android.os.Build
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var button: Button
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "Android Version"
textView = findViewById(R.id.textView)
button = findViewById(R.id.button)
button.setOnClickListener {
val apiLevel = Build.VERSION.SDK_INT
val versionName = getAndroidVersionName(apiLevel)
textView.text = "API Level: $apiLevel\nVersion: $versionName"
}
}
private fun getAndroidVersionName(apiLevel: Int): String {
return when (apiLevel) {
Build.VERSION_CODES.BASE -> "Android 1.0"
Build.VERSION_CODES.BASE_1_1 -> "Android 1.1"
Build.VERSION_CODES.CUPCAKE -> "Android 1.5 Cupcake"
Build.VERSION_CODES.DONUT -> "Android 1.6 Donut"
Build.VERSION_CODES.ECLAIR -> "Android 2.0/2.1 Eclair"
Build.VERSION_CODES.FROYO -> "Android 2.2 Froyo"
Build.VERSION_CODES.GINGERBREAD -> "Android 2.3 Gingerbread"
Build.VERSION_CODES.HONEYCOMB -> "Android 3.0 Honeycomb"
Build.VERSION_CODES.ICE_CREAM_SANDWICH -> "Android 4.0 Ice Cream Sandwich"
Build.VERSION_CODES.JELLY_BEAN -> "Android 4.1 Jelly Bean"
Build.VERSION_CODES.JELLY_BEAN_MR1 -> "Android 4.2 Jelly Bean"
Build.VERSION_CODES.JELLY_BEAN_MR2 -> "Android 4.3 Jelly Bean"
Build.VERSION_CODES.KITKAT -> "Android 4.4 KitKat"
Build.VERSION_CODES.LOLLIPOP -> "Android 5.0 Lollipop"
Build.VERSION_CODES.LOLLIPOP_MR1 -> "Android 5.1 Lollipop"
Build.VERSION_CODES.M -> "Android 6.0 Marshmallow"
Build.VERSION_CODES.N -> "Android 7.0 Nougat"
Build.VERSION_CODES.N_MR1 -> "Android 7.1 Nougat"
Build.VERSION_CODES.O -> "Android 8.0 Oreo"
Build.VERSION_CODES.O_MR1 -> "Android 8.1 Oreo"
Build.VERSION_CODES.P -> "Android 9 Pie"
Build.VERSION_CODES.Q -> "Android 10"
Build.VERSION_CODES.R -> "Android 11"
Build.VERSION_CODES.S -> "Android 12"
Build.VERSION_CODES.S_V2 -> "Android 12L"
Build.VERSION_CODES.TIRAMISU -> "Android 13"
Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> "Android 14"
Build.VERSION_CODES.VANILLA_ICE_CREAM -> "Android 15"
else -> "Unknown (API $apiLevel)"
}
}
}
ขั้นตอนที่ 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.androidversion">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ข้อมูลเพิ่มเติมจาก Build.VERSION
Build.VERSION.SDK_INT- API Level (Integer)Build.VERSION.RELEASE- เวอร์ชันที่ผู้ใช้เห็น (เช่น "13", "14")Build.VERSION.CODENAME- รหัสภายใน (เช่น "REL", "VanillaIceCream")Build.VERSION.INCREMENTAL- Build numberBuild.MODEL,Build.MANUFACTURER,Build.BRAND- ข้อมูลอุปกรณ์
การประยุกต์ใช้
ใช้สำหรับเงื่อนไขรันโค้ดเฉพาะเวอร์ชัน (Version-specific code) เช่น Permission Runtime, Scoped Storage, Foreground Service Types ฯลฯ