._ssaid">
赞
踩
- /**
- * 获取手机的设备号.
- * @param context 上下文
- * @return 设备号
- */
- @SuppressLint("HardwareIds")
- public static String getIMEIDeviceId(Context context) {
-
- String deviceId;
- //如果sdk版本大于等于29
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
- deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
- } else {
- final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
- return "";
- }
- }
- assert mTelephony != null;
- if (mTelephony.getDeviceId() != null) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- deviceId = mTelephony.getImei();
- } else {
- deviceId = mTelephony.getDeviceId();
- }
- } else {
- deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
- }
- }
-
- return deviceId;
- }

- if (context.applicationInfo.targetSdkVersion >= 29 && Build.VERSION.SDK_INT >= 29 ){
- //大于等于29使用特殊方法
- getUniqueID(context);
- }
- private fun getUniqueID(context: Context): String? {
- var id: String? = null
- val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
- if (!Tool.isEmpty(androidId) && "9774d56d682e549c" != androidId) {
- try {
- val uuid = UUID.nameUUIDFromBytes(androidId.toByteArray(charset("utf8")))
- id = uuid.toString()
- } catch (e: Exception) {
- e.printStackTrace()
- }
- }
- if (Tool.isEmpty(id)) {
- id = getUUID()
- }
- return if (Tool.isEmpty(id)) UUID.randomUUID().toString() else id
- }
- private fun getUUID(): String? {
- var serial: String? = null
- val m_szDevIDShort = "35" + Build.BOARD.length % 10 + Build.BRAND.length % 10 + (if (null != Build.CPU_ABI) Build.CPU_ABI.length else 0) % 10 + Build.DEVICE.length % 10 + Build.DISPLAY.length % 10 + Build.HOST.length % 10 + Build.ID.length % 10 + Build.MANUFACTURER.length % 10 + Build.MODEL.length % 10 + Build.PRODUCT.length % 10 + Build.TAGS.length % 10 + Build.TYPE.length % 10 + Build.USER.length % 10 //13 位
- if (Build.VERSION.SDK_INT <= 29) {
- try {
- serial = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- Build.getSerial()
- } else {
- Build.SERIAL
- }
- //API>=9 使用serial号
- return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
- } catch (exception: java.lang.Exception) {
- serial = "serial" // 随便一个初始化
- }
- } else {
- serial = Build.UNKNOWN // 随便一个初始化
- }
-
- //使用硬件信息拼凑出来的15位号码
- return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。