获取设备UUID
This commit is contained in:
parent
9067232e60
commit
9e12f98bb7
|
|
@ -1,6 +1,9 @@
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:name=".App"
|
android:name=".App"
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,90 @@
|
||||||
|
package com.ama.core.architecture.util
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import android.provider.Settings
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
|
|
||||||
|
class DeviceUtil private constructor() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* public method
|
||||||
|
*/
|
||||||
|
fun generateDeviceId(context: Context): String {
|
||||||
|
val hardwareUUID = getHardwareUUID()
|
||||||
|
if (hardwareUUID.isNotBlank()) {
|
||||||
|
return hardwareUUID
|
||||||
|
}
|
||||||
|
|
||||||
|
// way2
|
||||||
|
val androidId = getAndroidId(context)
|
||||||
|
if (androidId != null && isValidAndroidId(androidId)) {
|
||||||
|
return UUID.nameUUIDFromBytes(androidId.toByteArray()).toString()
|
||||||
|
}
|
||||||
|
|
||||||
|
// final:there's no other better way
|
||||||
|
return generateAndSaveRandomUUID(context)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WAY-1 : Got uuid without any permissions, suggest this way
|
||||||
|
*/
|
||||||
|
private fun getHardwareUUID(): String {
|
||||||
|
return try {
|
||||||
|
val hardwareInfo = StringBuilder().apply {
|
||||||
|
append(Build.BOARD)
|
||||||
|
append(Build.BRAND)
|
||||||
|
append(Build.DEVICE)
|
||||||
|
append(Build.HARDWARE)
|
||||||
|
append(Build.MANUFACTURER)
|
||||||
|
append(Build.MODEL)
|
||||||
|
append(Build.PRODUCT)
|
||||||
|
}.toString()
|
||||||
|
|
||||||
|
if (hardwareInfo.isNotBlank()) {
|
||||||
|
UUID.nameUUIDFromBytes(hardwareInfo.toByteArray()).toString()
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WAY-2:got android_ids
|
||||||
|
*/
|
||||||
|
private fun getAndroidId(context: Context): String? {
|
||||||
|
return try {
|
||||||
|
Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private fun isValidAndroidId(androidId: String): Boolean {
|
||||||
|
return androidId.isNotBlank() &&
|
||||||
|
androidId != "0000000000000000" &&
|
||||||
|
androidId != "9774d56d682e549c"
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* random uuid and save 2 sp to keep the same value
|
||||||
|
*/
|
||||||
|
private fun generateAndSaveRandomUUID(context: Context): String {
|
||||||
|
val prefs = context.getSharedPreferences("device_id_prefs", Context.MODE_PRIVATE)
|
||||||
|
var savedUuid = prefs.getString("device_uuid", null)
|
||||||
|
|
||||||
|
if (savedUuid == null) {
|
||||||
|
savedUuid = UUID.randomUUID().toString()
|
||||||
|
prefs.edit().putString("device_uuid", savedUuid).apply()
|
||||||
|
}
|
||||||
|
|
||||||
|
return savedUuid
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
package com.ama.core.architecture.util
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class SpUtil private constructor() {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue