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

Cách lấy và lưu Device ID trong Android

Hướng dẫn này trình bày cách lấy Android ID (mã định danh thiết bị duy nhất) và hiển thị nó trong ứng dụng. Android ID là một số hex 64-bit được tạo ngẫu nhiên khi thiết bị được thiết lập lần đầu (boot đầu tiên) và thường không thay đổi trừ khi factory reset.

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 và điền thông tin cần thiết.

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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textSize="24sp"
        android:textStyle="bold"/>

</RelativeLayout>

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

import android.provider.Settings;
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);

        // Lấy Android ID (Settings.Secure.ANDROID_ID)
        String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);

        textView.setText("Device ID: " + deviceId);
    }
}

Bước 4: Cấu hình 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. Ứng dụng sẽ hiển thị Android ID của thiết bị.

Cách lấy và lưu Device ID trong Android

Lưu ý quan trọng về Device ID

Android ID (Settings.Secure.ANDROID_ID)

  • Ưu điểm: Không cần quyền đặc biệt, dễ lấy, duy nhất trên mỗi cài đặt
  • Nhược điểm: Thay đổi sau factory reset; có thể bị giả mạo trên thiết bị root; từ Android 8.0 (API 26), mỗi app có Android ID riêng biệt (scoped per app)

Các định danh khác

Định danh API Yêu cầu quyền Độ bền
IMEI/MEID TelephonyManager.getImei() READ_PHONE_STATE Vĩnh viễn (gắn với phần cứng)
Serial Number Build.getSerial() READ_PHONE_STATE (API 26+) Vĩnh viễn
Advertising ID Google Play Services API Không (nhưng cần GPS) Người dùng có thể reset
Instance ID / Firebase Installation ID Firebase API Không Mỗi cài đặt app

Khuyến nghị thực tế

  • Dùng Firebase Installation ID hoặc Instance ID để nhận diện cài đặt app
  • Dùng Advertising ID cho quảng cáo/phân tích (tuân thủ chính sách Google Play)
  • Tránh dùng IMEI/Serial trừ khi cần thiết và có lý do chính đáng (chính sách Google Play hạn chế)
  • Lưu ID vào SharedPreferences hoặc database local nếu cần dùng lại