Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีตรวจสอบเวอร์ชัน Android (System Version) ในแอป

ภาพรวม

ตัวอย่างนี้แสดงวิธีการดึงและแสดงเวอร์ชัน Android ของอุปกรณ์ รวมถึง Code Name (ชื่อรหัส) และ API Level โดยใช้ Build.VERSION

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (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:padding="16dp"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAlignment="center"
        android:textSize="24sp"
        android:textStyle="bold" />
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (MainActivity.java)

package com.example.androidversion;

import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        textView = findViewById(R.id.textView);
        showAndroidVersion();
    }
    
    private void showAndroidVersion() {
        // ดึงข้อมูลเวอร์ชัน
        String release = Build.VERSION.RELEASE; // เช่น "13", "14"
        int sdkInt = Build.VERSION.SDK_INT;     // API Level เช่น 33, 34
        String codename = Build.VERSION.CODENAME; // เช่น "REL", "UpsideDownCake"
        
        // แปลง Release เป็นตัวเลขสำหรับเปรียบเทียบ
        double versionNumber = 0;
        try {
            versionNumber = Double.parseDouble(release.split("\\.")[0]);
        } catch (NumberFormatException ignored) {}
        
        // Map API Level เป็น Code Name (อัปเดตถึง Android 14)
        String marketingName = getMarketingName(sdkInt);
        
        String result = String.format(
            "Android Version: %s\nAPI Level: %d\nCode Name: %s\nMarketing Name: %s",
            release, sdkInt, codename, marketingName
        );
        
        textView.setText(result);
    }
    
    private String getMarketingName(int apiLevel) {
        return switch (apiLevel) {
            case 34 -> "Android 14 (UpsideDownCake)";
            case 33 -> "Android 13 (Tiramisu)";
            case 32 -> "Android 12L (Sv2)";
            case 31 -> "Android 12 (Snow Cone)";
            case 30 -> "Android 11 (Red Velvet Cake)";
            case 29 -> "Android 10 (Quince Tart)";
            case 28 -> "Android 9 (Pie)";
            case 27 -> "Android 8.1 (Oreo)";
            case 26 -> "Android 8.0 (Oreo)";
            case 25 -> "Android 7.1 (Nougat)";
            case 24 -> "Android 7.0 (Nougat)";
            case 23 -> "Android 6.0 (Marshmallow)";
            case 22 -> "Android 5.1 (Lollipop)";
            case 21 -> "Android 5.0 (Lollipop)";
            case 19 -> "Android 4.4 (KitKat)";
            case 18 -> "Android 4.3 (Jelly Bean)";
            case 17 -> "Android 4.2 (Jelly Bean)";
            case 16 -> "Android 4.1 (Jelly Bean)";
            default -> "Unknown / Future Version";
        };
    }
}

ขั้นตอนที่ 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>

การทดสอบ

รันแอปพลิเคชันบนอุปกรณ์จริง จะแสดง Version, API Level, Code Name และ Marketing Name

วิธีตรวจสอบเวอร์ชัน Android (System Version) ในแอป

ข้อมูล Build.VERSION ที่มีประโยชน์

Fieldคำอธิบายตัวอย่าง
VERSION.RELEASEเวอร์ชันที่ผู้ใช้เห็น"14", "13", "12"
VERSION.SDK_INTAPI Level (สำคัญสำหรับ Dev)34, 33, 32
VERSION.CODENAMEรหัสภายใน (REL = Release)"REL", "UpsideDownCake"
VERSION.INCREMENTALBuild Number เฉพาะอุปกรณ์"TP1A.220624.014"
VERSION.SECURITY_PATCHวันที่ Security Patch"2023-10-05"
VERSION.BASE_OSBase OS Version""
DEVICEรหัสอุปกรณ์"cheetah" (Pixel 7 Pro)
MODELชื่อรุ่นตลาด"Pixel 7 Pro"
MANUFACTURERผู้ผลิต"Google"
BRANDแบรนด์"google"
PRODUCTชื่อผลิตภัณฑ์"cheetah"

การใช้งานจริง (Version Check)

// ตรวจสอบ API Level ก่อนใช้ Feature ใหม่
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // API 33+
    // ใช้ Photo Picker API ใหม่
} else {
    // Fallback ใช้ Intent.ACTION_PICK เดิม
}

// หรือใช้ androidx.core.os.BuildCompat
if (BuildCompat.isAtLeastT()) { // Android 13+
    // ...
}