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

Cách lấy số phiên bản Android (API Level) bằng lập trình

Giới thiệu

Hướng dẫn này trình bày cách lấy số phiên bản Android (API Level / SDK INT) của thiết bị đang chạy ứng dụng. Thông tin này quan trọng để kiểm tra tương thích tính năng theo phiên bản.

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

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

Tạo dự án Android Studio: File → New Project.

Bước 2: 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">

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

</LinearLayout>

Bước 3: MainActivity.java

package com.example.myapplication;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
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);

        // QUAN TRỌNG: Build.VERSION.SDK_INT KHÔNG CẦN QUYỀN READ_PHONE_STATE
        // Code gốc sai khi yêu cầu quyền này cho SDK_INT
        textView.setText("API Level: " + Build.VERSION.SDK_INT);
        textView.append("\nAndroid Version: " + Build.VERSION.RELEASE);
        textView.append("\nBuild ID: " + Build.ID);
    }
}

Bước 4: AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET"/>
    <!-- KHÔNG CẦN READ_PHONE_STATE CHO SDK_INT -->

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

Sửa lỗi quan trọng từ code gốc

Code gốc yêu cầu quyền READ_PHONE_STATE để đọc Build.VERSION.SDK_INT - ĐÂY LÀ SAI.

  • Build.VERSION.SDK_INT là hằng số công khai, không cần quyền đặc biệt.
  • Build.VERSION.RELEASE (chuỗi phiên bản như "13", "14") cũng không cần quyền.
  • Quyền READ_PHONE_STATE chỉ cần cho IMEI, số điện thoại, serial số thiết bị.

Bảng phiên bản Android phổ biến

API LevelPhiên bảnTên mãNăm
3414UpsideDownCake2023
3313Tiramisu2022
3212LSv22022
3112S2021
3011R2020
2910Q2019
289Pie2018
278.1Oreo2017
268.0Oreo2017
236.0Marshmallow2015
215.0Lollipop2014

Ứng dụng thực tế

// Kiểm tra phiên bản trước khi dùng API mới
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // API 23+
    // Dùng API yêu cầu runtime permission
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // API 24+
    // Dùng Multi-window, Direct Reply notification
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // API 29+
    // Dùng Scoped Storage, Dark Theme
}