How to get device UUID without permission
本问题已经有最佳答案,请猛点这里访问。
我想在Android中获得一个设备UUID,一个应用程序的唯一标识符。怎么能做到?希望我不需要任何权限。
这将为您提供唯一的设备ID:
1 2 3 4 | import android.provider.Settings.Secure; private String androidId = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID); |
更多信息在这里。
我使用了这个方法https://stackoverflow.com/a/42673369/3172843,并做了一些更改:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | public static String generateDeviceIdentifier(Context context) { String pseudoId ="35" + Build.BOARD.length() % 10 + Build.BRAND.length() % 10 + Build.CPU_ABI.length() % 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; String androidId = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); String longId = pseudoId + androidId; try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); messageDigest.update(longId.getBytes(), 0, longId.length()); // get md5 bytes byte md5Bytes[] = messageDigest.digest(); // creating a hex string String identifier =""; for (byte md5Byte : md5Bytes) { int b = (0xFF & md5Byte); // if it is a single digit, make sure it have 0 in front (proper padding) if (b <= 0xF) { identifier +="0"; } // add number to string identifier += Integer.toHexString(b); } // hex string to uppercase identifier = identifier.toUpperCase(); return identifier; } catch (Exception e) { return UUID.randomUUID().toString(); } } |