บทนำ
ตัวอย่างนี้แสดงวิธีดึงข้อมูลเวอร์ชัน Android ของอุปกรณ์รวมถึง API Level และ Version Name ผ่าน 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 SDK Version"
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_centerInParent="true"
android:textColor="@android:color/black"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
ขั้นตอนที่ 3: MainActivity.kt
import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
private lateinit var textView: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "SDK Version Info"
textView = findViewById(R.id.textView)
val versionAPI = Build.VERSION.SDK_INT
val versionRelease = Build.VERSION.RELEASE
val versionCodename = Build.VERSION.CODENAME
val versionIncremental = Build.VERSION.INCREMENTAL
val versionSecurityPatch = Build.VERSION.SECURITY_PATCH
val info = buildString {
appendLine("Android Version Information")
appendLine("============================")
appendLine("API Level (SDK_INT): $versionAPI")
appendLine("Version Release: $versionRelease")
appendLine("Codename: $versionCodename")
appendLine("Incremental: $versionIncremental")
appendLine("Security Patch: $versionSecurityPatch")
appendLine()
appendLine("Device Info:")
appendLine("Manufacturer: ${Build.MANUFACTURER}")
appendLine("Brand: ${Build.BRAND}")
appendLine("Model: ${Build.MODEL}")
appendLine("Device: ${Build.DEVICE}")
appendLine("Product: ${Build.PRODUCT}")
appendLine("Hardware: ${Build.HARDWARE}")
appendLine("Fingerprint: ${Build.FINGERPRINT}")
}
textView.text = info
}
}
ขั้นตอนที่ 4: Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.sdkversion">
<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 Fields สำคัญ
| Field | Type | คำอธิบาย | ตัวอย่าง |
|---|---|---|---|
SDK_INT | Int | API Level (Integer) | 34 (Android 14) |
RELEASE | String | เวอร์ชันที่ผู้ใช้เห็น | "14", "13", "12L" |
CODENAME | String | รหัสภายในระหว่างพัฒนา | "REL", "VanillaIceCream" |
INCREMENTAL | String | Build Number | "UP1A.231005.007" |
SECURITY_PATCH | String | วันที่ Security Patch (YYYY-MM-DD) | "2024-01-05" |
BASE_OS | String | Base OS version | "" |
PREVIEW_SDK_INT | Int | Preview SDK Level (ถ้าเป็น Beta) | 0 หรือ > SDK_INT |
Build.VERSION_CODES Constants
ใช้สำหรับตรวจสอบเวอร์ชันในโค้ด:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Android 13+ (API 33+)
// ใช้ Photo Picker, Media Permissions ใหม่, Foreground Service Types
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12+ (API 31+)
// ใช้ Splash Screen API, Haptic Feedback constants
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Android 11+ (API 30+)
// ใช้ Scoped Storage, One-time permissions
}
การใช้งานจริง (Real-world Usage)
- Runtime Permissions - ตรวจสอบ API 23+ ก่อนขอ Permission
- Scoped Storage - API 29+ จำกัดการเข้าถึงไฟล์
- Foreground Services - API 26+ ต้องมี Notification, API 34+ ต้องระบุ Type
- Notification Channels - API 26+ บังคับ Channel
- Biometric Prompt - API 28+ ใช้ BiometricPrompt แทน FingerprintManager
- JobScheduler / WorkManager - API 21+ / 23+ สำหรับ Background Work
Extension Function สะดวก
fun Context.getAndroidVersionName(): String {
return when (Build.VERSION.SDK_INT) {
Build.VERSION_CODES.VANILLA_ICE_CREAM -> "Android 15"
Build.VERSION_CODES.UPSIDE_DOWN_CAKE -> "Android 14"
Build.VERSION_CODES.TIRAMISU -> "Android 13"
Build.VERSION_CODES.S_V2 -> "Android 12L"
Build.VERSION_CODES.S -> "Android 12"
Build.VERSION_CODES.R -> "Android 11"
Build.VERSION_CODES.Q -> "Android 10"
Build.VERSION_CODES.P -> "Android 9 Pie"
Build.VERSION_CODES.O_MR1 -> "Android 8.1 Oreo"
Build.VERSION_CODES.O -> "Android 8.0 Oreo"
Build.VERSION_CODES.N_MR1 -> "Android 7.1 Nougat"
Build.VERSION_CODES.N -> "Android 7.0 Nougat"
Build.VERSION_CODES.M -> "Android 6.0 Marshmallow"
Build.VERSION_CODES.LOLLIPOP_MR1 -> "Android 5.1 Lollipop"
Build.VERSION_CODES.LOLLIPOP -> "Android 5.0 Lollipop"
Build.VERSION_CODES.KITKAT -> "Android 4.4 KitKat"
// ... เพิ่มต่อไปตามต้องการ
else -> "Android ${Build.VERSION.RELEASE} (API ${Build.VERSION.SDK_INT})"
}
}