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

Cách kiểm tra mẫu mã điện thoại Android bằng lập trình?

Giới thiệu

Bài viết minh họa cách lấy tên mẫu mã thiết bị (Model) Android thông qua lớp android.os.Build.

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

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

Tạo project Android Studio tiêu chuẩn.

Bước 2: Giao diện hiển thị model (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:layout_margin="16dp"
    tools:context=".MainActivity">
    <TextView
        android:gravity="center"
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="18sp" />
</RelativeLayout>

Bước 3: Logic lấy model (src/MainActivity.java)

package app.tutorialspoint.com.sample;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.textView);
        textView.setText(getPhoneModel());
    }

    private String getPhoneModel() {
        return Build.MODEL;
    }
}

Bước 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.tutorialspoint.com.sample">
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <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ị thật, nhấn Run. Màn hình sẽ hiển thị mẫu mã thiết bị (ví dụ: "SM-G973F", "Pixel 5", "Redmi Note 9").

Cách kiểm tra mẫu mã điện thoại Android bằng lập trình?

Các thông tin Build hữu ích khác

  • Build.MANUFACTURER - Nhà sản xuất (Samsung, Google, Xiaomi...)
  • Build.BRAND - Thương hiệu
  • Build.DEVICE - Tên mã thiết bị
  • Build.VERSION.RELEASE - Phiên bản Android (11, 12, 13...)
  • Build.VERSION.SDK_INT - API Level