ImeiUtil.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.yyrh.imei;
  2. import android.Manifest;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.content.SharedPreferences;
  6. import android.content.pm.PackageManager;
  7. import android.os.Build;
  8. import android.provider.Settings;
  9. import android.support.v4.app.ActivityCompat;
  10. import android.telephony.TelephonyManager;
  11. import com.yyrh.constant.Constants;
  12. import com.yyrh.constant.SDKSettings;
  13. import com.yyrh.utils.Utils;
  14. import com.yythird.sdk.ChannelSDK;
  15. public class ImeiUtil {
  16. public static String getImei(Context context) {
  17. TelephonyManager telephonyManager = null;
  18. String imei = "";
  19. try {
  20. telephonyManager = (TelephonyManager) context
  21. .getSystemService(Context.TELEPHONY_SERVICE);
  22. if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
  23. // TODO: Consider calling
  24. // ActivityCompat#requestPermissions
  25. // here to request the missing permissions, and then overriding
  26. // public void onRequestPermissionsResult(int requestCode, String[] permissions,
  27. // int[] grantResults)
  28. // to handle the case where the user grants the permission. See the documentation
  29. // for ActivityCompat#requestPermissions for more details.
  30. imei = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
  31. if (judgeStrNull(imei)){
  32. return userSimulateImei(context);
  33. }else{
  34. SDKSettings.ifa_type = "3";
  35. return imei;
  36. }
  37. }
  38. if (telephonyManager.getDeviceId() != null) {
  39. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  40. imei = telephonyManager.getImei();
  41. }else{
  42. imei = telephonyManager.getDeviceId();
  43. }
  44. SDKSettings.ifa_type = "1";
  45. } else {
  46. imei = Settings.Secure.getString(context.getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
  47. SDKSettings.ifa_type = "3";
  48. }
  49. } catch (Exception e) {
  50. System.out.println(e.toString());
  51. }
  52. if (judgeStrNull(imei)){
  53. return userSimulateImei(context);
  54. }else{
  55. Utils.setSharedPreferences("yyrh","simulateImei",imei,context);
  56. }
  57. return imei;
  58. }
  59. static String userSimulateImei(Context context){
  60. SharedPreferences sp = context.getSharedPreferences("yyrh", Activity.MODE_PRIVATE);
  61. String simulateImei = sp.getString("simulateImei", "");
  62. if (!judgeStrNull(simulateImei)){
  63. return simulateImei;
  64. }else {
  65. // 用putString的方法保存数据
  66. String encryptMD5to16 ="YYR" + encryptMD5to16();
  67. SDKSettings.ifa_type = "0";
  68. Utils.setSharedPreferences("yyrh","simulateImei",encryptMD5to16,context);
  69. return encryptMD5to16;
  70. }
  71. }
  72. public static boolean judgeStrNull(String str) {
  73. return (str == null || str.equals(""));
  74. }
  75. public static String encryptMD5to16() {
  76. return simulateUniqueId().substring(8, 24);
  77. }
  78. //获得独一无二的Psuedo ID
  79. public static String simulateUniqueId() {
  80. String serial = null;
  81. String m_szDevIDShort = "" +
  82. Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
  83. Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
  84. Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
  85. Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
  86. Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
  87. Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
  88. Build.USER.length() % 10; //13 位
  89. String time = m_szDevIDShort + (System.currentTimeMillis());
  90. return getMD5(time);
  91. }
  92. public static String getMD5(String content) {
  93. String s = null;
  94. char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  95. 'a', 'b', 'c', 'd', 'e', 'f'};
  96. try {
  97. java.security.MessageDigest md = java.security.MessageDigest
  98. .getInstance("MD5");
  99. md.update(content.getBytes());
  100. byte tmp[] = md.digest();
  101. char str[] = new char[16 * 2];
  102. int k = 0;
  103. for (int i = 0; i < 16; i++) {
  104. byte byte0 = tmp[i];
  105. str[k++] = hexDigits[byte0 >>> 4 & 0xf];
  106. str[k++] = hexDigits[byte0 & 0xf];
  107. }
  108. s = new String(str);
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. return s;
  113. }
  114. public void getOaid(final Context context){
  115. ChannelSDK.getInstance().SDKGetOaid();
  116. }
  117. }