Hướng dẫn này minh họa cách lấy và lưu Device ID (Android ID) trong Android sử dụng Kotlin.
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: Thiết kế giao diện (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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="@android:color/holo_purple"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
Bước 3: Viết code MainActivity.kt
import android.os.Bundle
import android.provider.Settings
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val textView: TextView = findViewById(R.id.textView)
val id: String = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
textView.text = "Device ID: $id"
}
}
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="com.example.q11">
<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. TextView sẽ hiển thị Android ID duy nhất của thiết bị (ví dụ: a1b2c3d4e5f6g7h8).
Lưu ý quan trọng về Device ID
- Android ID (Settings.Secure.ANDROID_ID): Được reset khi factory reset, có thể thay đổi sau Android 8.0 (API 26) cho từng app/package.
- Không dùng IMEI: Yêu cầu quyền
READ_PHONE_STATEvà bị hạn chế từ Android 10 (API 29). - Lưu trữ bền vững: Nên lưu Android ID vào
SharedPreferenceshoặc database để dùng lại. - Best practice: Kết hợp Android ID + UUID tự sinh + Firebase Installation ID để định danh thiết bị tin cậy hơn.
Ví dụ lưu vào SharedPreferences:
val prefs = getSharedPreferences("device_prefs", MODE_PRIVATE)
val deviceId = prefs.getString("device_id", null) ?: run {
val id = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID)
prefs.edit().putString("device_id", id).apply()
id
}