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

Cách lấy phiên bản Android SDK hiện tại bằng code

Giới thiệu

Ví dụ này minh họa cách lấy phiên bản Android SDK (API level) và phiên bản phát hành (release version) của thiết bị đang chạy ứng dụng theo chương trình.

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

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

Mở Android Studio, chọn File → New Project, điền thông tin tạo dự án.

Bước 2: Thiết kế giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="30dp"
        android:textSize="24sp"
        android:textStyle="bold" />

</LinearLayout>

Bước 3: Viết code logic (src/MainActivity.java)

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

        int versionAPI = Build.VERSION.SDK_INT;
        String versionRelease = Build.VERSION.RELEASE;

        textView.setText("API Version: " + versionAPI + "\n" + "Version Release: " + versionRelease);
    }
}

Bước 4: Cấu hình Manifest (androidManifest.xml)

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

Kết nối thiết bị, nhấn Run. Màn hình sẽ hiển thị API Level (ví dụ: 33) và phiên bản Android (ví dụ: 13).

Cách lấy phiên bản Android SDK hiện tại bằng code

Thông tin bổ sung hữu ích

Class Build.VERSION cung cấp nhiều hằng số hữu ích khác:

  • Build.VERSION.SDK_INT: API Level (integer) - dùng để kiểm tra tính năng có sẵn hay không.
  • Build.VERSION.RELEASE: Chuỗi phiên bản người dùng thấy (VD: "13", "12.1").
  • Build.VERSION.CODENAME: Tên mã phát hành (VD: "REL" cho bản release, "Tiramisu" cho Android 13 preview).
  • Build.VERSION.INCREMENTAL: Số build nội bộ.
  • Build.MANUFACTURER, Build.MODEL, Build.BRAND: Thông tin thiết bị.

Ứng dụng thực tế

Thường dùng để:

  • Kiểm tra phiên bản trước khi gọi API mới (runtime permission, notification channel, v.v.)
  • Ghi log/analytics phiên bản thiết bị người dùng.
  • Bật/tắt tính năng tùy theo API level.