获取android设备唯一识别码

2019-01-15  本文已影响0人  Sunny君907

在个别机型下面,通过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

上一篇下一篇

猜你喜欢

热点阅读