获取android设备唯一识别码

在个别机型下面,通过telephonyManager获取deviceId为空,可通过Setting.Secure方式获取android设备唯一识别码

@Suppress("DEPRECATION")
@SuppressLint("HardwareIds")
fun getDeviceId(context: Context?) : String?{
    var deviceId: String? = null
    try {
        deviceId = (context?.applicationContext?.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager).deviceId
        if (deviceId == null) {
            deviceId = Settings.Secure.getString(context.applicationContext.contentResolver, Settings.Secure.ANDROID_ID)
        }
    } catch (e: Exception){

    } finally {
        return deviceId
    }
}

参考链接:https://stackoverflow.com/questions/3802644/will-telephonymanger-getdeviceid-return-device-id-for-tablets-like-galaxy-tab

你可能感兴趣的:(获取android设备唯一识别码)