在个别机型下面,通过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
}
}
网友评论