._ssaid">
当前位置:   article > 正文

Android获取设备号SSAID (Android ID) 和 IMEI

ssaid

1、基本方式 

  1. /**
  2. * 获取手机的设备号.
  3. * @param context 上下文
  4. * @return 设备号
  5. */
  6. @SuppressLint("HardwareIds")
  7. public static String getIMEIDeviceId(Context context) {
  8. String deviceId;
  9. //如果sdk版本大于等于29
  10. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  11. deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
  12. } else {
  13. final TelephonyManager mTelephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
  14. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  15. if (context.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  16. return "";
  17. }
  18. }
  19. assert mTelephony != null;
  20. if (mTelephony.getDeviceId() != null) {
  21. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  22. deviceId = mTelephony.getImei();
  23. } else {
  24. deviceId = mTelephony.getDeviceId();
  25. }
  26. } else {
  27. deviceId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
  28. }
  29. }
  30. return deviceId;
  31. }

 2、官方推荐

  1. if (context.applicationInfo.targetSdkVersion >= 29 && Build.VERSION.SDK_INT >= 29 ){
  2. //大于等于29使用特殊方法
  3. getUniqueID(context);
  4. }
  5. private fun getUniqueID(context: Context): String? {
  6. var id: String? = null
  7. val androidId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
  8. if (!Tool.isEmpty(androidId) && "9774d56d682e549c" != androidId) {
  9. try {
  10. val uuid = UUID.nameUUIDFromBytes(androidId.toByteArray(charset("utf8")))
  11. id = uuid.toString()
  12. } catch (e: Exception) {
  13. e.printStackTrace()
  14. }
  15. }
  16. if (Tool.isEmpty(id)) {
  17. id = getUUID()
  18. }
  19. return if (Tool.isEmpty(id)) UUID.randomUUID().toString() else id
  20. }
  21. private fun getUUID(): String? {
  22. var serial: String? = null
  23. 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 位
  24. if (Build.VERSION.SDK_INT <= 29) {
  25. try {
  26. serial = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  27. Build.getSerial()
  28. } else {
  29. Build.SERIAL
  30. }
  31. //API>=9 使用serial号
  32. return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
  33. } catch (exception: java.lang.Exception) {
  34. serial = "serial" // 随便一个初始化
  35. }
  36. } else {
  37. serial = Build.UNKNOWN // 随便一个初始化
  38. }
  39. //使用硬件信息拼凑出来的15位号码
  40. return UUID(m_szDevIDShort.hashCode().toLong(), serial.hashCode().toLong()).toString()
  41. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/270895
推荐阅读
相关标签
  

闽ICP备14008679号