Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách kiểm tra phiên bản hệ điều hành Android (API Level, Version Name)

Giới thiệu

Bài viết này hướng dẫn cách lấy thông tin phiên bản Android đang chạy trên thiết bị: API Level (SDK_INT), Version Name (Release), và Code Name (Tên gọi như Pie, Oreo...).

Các bước thực hiện

Bước 1: Tạo dự án mới

Khởi tạo project Android Studio mới.

Bước 2: Layout (res/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:padding="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tvVersionInfo"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAlignment="center"
        android:textSize="20sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: Code Java (src/MainActivity.java)

package app.com.sample;

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

public class MainActivity extends AppCompatActivity {

    TextView tvVersionInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tvVersionInfo = findViewById(R.id.tvVersionInfo);
        displayVersionInfo();
    }

    private void displayVersionInfo() {
        // 1. API Level (SDK_INT) - Quan trọng nhất để check version tại runtime
        int apiLevel = Build.VERSION.SDK_INT;

        // 2. Version Name (VD: "13", "12.1.0", "11.0.0")
        String versionRelease = Build.VERSION.RELEASE;

        // 3. Code Name (Tên bánh ngọt) - Chỉ có sẵn từ API 24+ (Nougat)
        String codeName = getAndroidCodeName(apiLevel);

        // 4. Thông tin bổ sung
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        String brand = Build.BRAND;
        String hardware = Build.HARDWARE;
        String device = Build.DEVICE;
        String product = Build.PRODUCT;

        // 5. Security Patch
        String securityPatch = Build.VERSION.SECURITY_PATCH;

        // 6. Build ID / Fingerprint
        String buildId = Build.ID;
        String fingerprint = Build.FINGERPRINT;

        StringBuilder info = new StringBuilder();
        info.append("📱 Thông tin thiết bị\n\n");
        info.append("API Level: ").append(apiLevel).append("\n");
        info.append("Version: ").append(versionRelease).append(" (").append(codeName).append(")\n");
        info.append("Thương hiệu: ").append(brand).append("\n");
        info.append("Nhà sản xuất: ").append(manufacturer).append("\n");
        info.append("Model: ").append(model).append("\n");
        info.append("Device: ").append(device).append("\n");
        info.append("Product: ").append(product).append("\n");
        info.append("Hardware: ").append(hardware).append("\n");
        info.append("Build ID: ").append(buildId).append("\n");
        info.append("Security Patch: ").append(securityPatch).append("\n");
        info.append("Fingerprint: ").append(fingerprint);

        tvVersionInfo.setText(info.toString());
    }

    // Map API Level -> Code Name
    private String getAndroidCodeName(int apiLevel) {
        if (apiLevel >= 34) return "Android 14 (UpsideDownCake)";
        if (apiLevel >= 33) return "Android 13 (Tiramisu)";
        if (apiLevel >= 32) return "Android 12L / 13 (Sv2)";
        if (apiLevel >= 31) return "Android 12 (Snow Cone)";
        if (apiLevel >= 30) return "Android 11 (Red Velvet Cake)";
        if (apiLevel >= 29) return "Android 10 (Quince Tart)";
        if (apiLevel >= 28) return "Android 9 (Pie)";
        if (apiLevel >= 27) return "Android 8.1 (Oreo MR1)";
        if (apiLevel >= 26) return "Android 8.0 (Oreo)";
        if (apiLevel >= 25) return "Android 7.1 (Nougat MR1)";
        if (apiLevel >= 24) return "Android 7.0 (Nougat)";
        if (apiLevel >= 23) return "Android 6.0 (Marshmallow)";
        if (apiLevel >= 22) return "Android 5.1 (Lollipop MR1)";
        if (apiLevel >= 21) return "Android 5.0 (Lollipop)";
        if (apiLevel >= 20) return "Android 4.4W (KitKat Watch)";
        if (apiLevel >= 19) return "Android 4.4 (KitKat)";
        if (apiLevel >= 18) return "Android 4.3 (Jelly Bean MR2)";
        if (apiLevel >= 17) return "Android 4.2 (Jelly Bean MR1)";
        if (apiLevel >= 16) return "Android 4.1 (Jelly Bean)";
        if (apiLevel >= 15) return "Android 4.0.3 (Ice Cream Sandwich MR1)";
        if (apiLevel >= 14) return "Android 4.0 (Ice Cream Sandwich)";
        return "Android " + apiLevel + " (Pre-ICS)";
    }
}

Các constant quan trọng trong Build.VERSION:

ConstantKiểuMô tảVí dụ
SDK_INTintAPI Level (dùng để check version)33, 34, 29...
RELEASEStringVersion name người dùng thấy"13", "12.1.0"
CODENAMEStringCode name nội bộ (REL = release)"REL", "Tiramisu"
INCREMENTALStringBuild number nội bộ"1234567"
SECURITY_PATCHStringNgày bản vá bảo mật"2023-12-05"
BASE_OSStringBase OS fingerprint...

Bước 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">

    <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>

Chạy ứng dụng

Chạy app, TextView hiển thị đầy đủ thông tin phiên bản Android của thiết bị.

Cách kiểm tra phiên bản hệ điều hành Android (API Level, Version Name)

Best Practice: Version Check Pattern

// ❌ KHÔNG NÊN: Check version name string
if (Build.VERSION.RELEASE.startsWith("13")) { ... }

// ✅ NÊN: Check SDK_INT (API Level)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // API 33+
    // Code dành cho Android 13+
}

// ✅ Sử dụng Build.VERSION_CODES constants
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { // API 31 (Android 12)
    // SplashScreen API, Material You...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // API 30 (Android 11)
    // Scoped Storage, One-time permissions...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // API 29 (Android 10)
    // Dark Theme, Gesture Navigation...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { // API 28 (Android 9)
    // BiometricPrompt, Network Security Config...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // API 26 (Android 8)
    // Notification Channels, Picture-in-Picture...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // API 23 (Android 6)
    // Runtime Permissions, Doze Mode...
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // API 21 (Android 5)
    // Material Design, ART Runtime, RecyclerView...
}

Kotlin Extension

// Extension properties
val Context.apiLevel: Int get() = Build.VERSION.SDK_INT
val Context.isAtLeastTiramisu: Boolean get() = apiLevel >= Build.VERSION_CODES.TIRAMISU
val Context.isAtLeastS: Boolean get() = apiLevel >= Build.VERSION_CODES.S
val Context.isAtLeastR: Boolean get() = apiLevel >= Build.VERSION_CODES.R
val Context.isAtLeastQ: Boolean get() = apiLevel >= Build.VERSION_CODES.Q
val Context.isAtLeastP: Boolean get() = apiLevel >= Build.VERSION_CODES.P
val Context.isAtLeastO: Boolean get() = apiLevel >= Build.VERSION_CODES.O
val Context.isAtLeastN: Boolean get() = apiLevel >= Build.VERSION_CODES.N
val Context.isAtLeastM: Boolean get() = apiLevel >= Build.VERSION_CODES.M
val Context.isAtLeastLollipop: Boolean get() = apiLevel >= Build.VERSION_CODES.LOLLIPOP

// Usage
if (context.isAtLeastTiramisu) { /* Android 13+ features */ }
if (context.isAtLeastS) { /* Android 12+ features */ }

Bảng tra cứu API Level ↔ Version Name ↔ Code Name

API LevelVersionCode NameNgày phát hành
3414UpsideDownCakeOct 2023
3313TiramisuAug 2022
3212LSv2Mar 2022
3112Snow ConeOct 2021
3011Red Velvet CakeSep 2020
2910Quince TartSep 2019
289PieAug 2018
278.1Oreo MR1Dec 2017
268.0OreoAug 2017
257.1Nougat MR1Oct 2016
247.0NougatAug 2016
236.0MarshmallowOct 2015
225.1Lollipop MR1Mar 2015
215.0LollipopNov 2014
19-204.4KitKatOct 2013
16-184.1-4.3Jelly BeanJul 2012
14-154.0Ice Cream SandwichOct 2011