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

วิธีดึง Device SDK Version ใน Android

การดึง Device SDK Version (OS Version) ใน Android

ตัวอย่างสาธิตการดึงเวอร์ชัน SDK ของอุปกรณ์ - หมายเหตุ: โค้ดต้นฉบับใช้ getDeviceSoftwareVersion() ซึ่งผิดวัตถุประสงค์ เราจะแก้ไขให้ถูกต้อง

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

Android Studio → File → New Project

ขั้นตอนที่ 2: Layout XML (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

ขั้นตอนที่ 3: โค้ด Java (src/MainActivity.java) - แก้ไขแล้ว

package com.example.myapplication;

import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

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.text);

        // === วิธีที่ถูกต้อง: ใช้ Build.VERSION (ไม่ต้อง Permission) ===
        StringBuilder info = new StringBuilder();
        info.append("SDK Version: ").append(Build.VERSION.SDK_INT).append("\n");        // API Level (integer)
        info.append("Release: ").append(Build.VERSION.RELEASE).append("\n");            // Version string (e.g., "10", "11")
        info.append("Codename: ").append(Build.VERSION.CODENAME).append("\n");          // Codename (e.g., "REL", "S")
        info.append("Incremental: ").append(Build.VERSION.INCREMENTAL).append("\n");    // Build number
        info.append("Preview SDK: ").append(Build.VERSION.PREVIEW_SDK_INT).append("\n");
        info.append("Security Patch: ").append(Build.VERSION.SECURITY_PATCH).append("\n");
        info.append("Base OS: ").append(Build.VERSION.BASE_OS).append("\n");

        // Device Info (จาก Build class)
        info.append("\n--- Device Info ---\n");
        info.append("Brand: ").append(Build.BRAND).append("\n");
        info.append("Manufacturer: ").append(Build.MANUFACTURER).append("\n");
        info.append("Model: ").append(Build.MODEL).append("\n");
        info.append("Device: ").append(Build.DEVICE).append("\n");
        info.append("Product: ").append(Build.PRODUCT).append("\n");
        info.append("Hardware: ").append(Build.HARDWARE).append("\n");
        info.append("Board: ").append(Build.BOARD).append("\n");
        info.append("Bootloader: ").append(Build.BOOTLOADER).append("\n");
        info.append("Fingerprint: ").append(Build.FINGERPRINT).append("\n");
        info.append("Tags: ").append(Build.TAGS).append("\n");
        info.append("Type: ").append(Build.TYPE).append("\n"); // user, userdebug, eng

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

❌ ข้อผิดพลาดของโค้ดต้นฉบับ

โค้ดต้นฉบับใช้: telephonyManager.getDeviceSoftwareVersion()
ปัญหา: เมธอดนี้คืนค่า Modem/Baseband Firmware Version (เช่น "MPSS.DI.2.0.c1.1-00123") ไม่ใช่ Android SDK Version
และต้อง Permission READ_PHONE_STATE ไม่จำเป็น

✅ วิธีที่ถูกต้อง: Build.VERSION (ไม่ต้อง Permission ใดๆ)

Fieldประเภทตัวอย่างคำอธิบาย
SDK_INTint29API Level (29 = Android 10)
RELEASEString"10"Version ที่ User เห็น
CODENAMEString"REL"Code name (REL=Release)
INCREMENTALString"QP1A.190711.020"Build number
SECURITY_PATCHString"2020-01-05"Security patch date
PREVIEW_SDK_INTint0Preview SDK version
BASE_OSString""Base OS build fingerprint

การทดสอบ

รันแอป จะแสดงข้อมูล SDK และ Device ทั้งหมดโดยไม่ต้องขอ Permission

วิธีดึง Device SDK Version ใน Android

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

// ตรวจสอบ API Level
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // Android 11+
    // ใช้ API ใหม่
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Android 10
    // Fallback
}

// ตรวจสอบ Preview/Development Build
if (Build.VERSION.CODENAME.equals("REL")) {
    // Release build
} else {
    // Beta/Preview build
}

TelephonyManager.getDeviceSoftwareVersion() คืออะไร?

  • คืนค่า Baseband/Modem Firmware Version
  • เช่น: "MPSS.DI.2.0.c1.1-00123", "MOLY.LR12A.R3.MP.V36.12"
  • ใช้สำหรับ Debug Network/Radio issues
  • ไม่ใช่ Android OS Version

สรุป

ใช้ Build.VERSION.SDK_INT และ Build.VERSION.RELEASE สำหรับ Android SDK Version - ไม่ต้อง Permission, เร็ว, ถูกต้อง