美文网首页
Android 创建唯一标识UUID

Android 创建唯一标识UUID

作者: 不会弹钢琴de大叔 | 来源:发表于2024-01-28 15:17 被阅读0次

    使用该方法创建当前应用内唯一识别码

    
    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.net.wifi.WifiManager;
    import android.os.Build;
    import android.provider.Settings.Secure;
    
    import java.nio.charset.StandardCharsets;
    import java.util.Map;
    import java.util.UUID;
    
    public class DeviceUuidFactory {
        protected static final String PREFS_FILE = "device_id.xml";
        protected static final String PREFS_DEVICE_ID = "device_id";
        protected static UUID uuid;
    
        @SuppressLint("HardwareIds")
        public DeviceUuidFactory(Context context) {
            if (uuid == null) {
                final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);
                final String id = prefs.getString(PREFS_DEVICE_ID, null);
                if (id != null) {
                    uuid = UUID.fromString(id);
                } else {
                    final String androidId = Secure.getString(context.getContentResolver(),
                            Secure.ANDROID_ID);
                    if (!"9774d56d682e549c".equals(androidId)) {
                        uuid = UUID.nameUUIDFromBytes(androidId
                                .getBytes(StandardCharsets.UTF_8));
                    } else {
                        uuid = generateDeviceUuid(context);
                    }
                    prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).apply();
                }
            }
        }
    
        @SuppressLint("HardwareIds")
        private UUID generateDeviceUuid(Context context) {
            String buildParams = Build.BOARD + Build.BRAND + Build.CPU_ABI
                    + Build.DEVICE + Build.DISPLAY + Build.FINGERPRINT + Build.HOST
                    + Build.ID + Build.MANUFACTURER + Build.MODEL + Build.PRODUCT
                    + Build.TAGS + Build.TYPE + Build.USER;
            String androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
            // requires ACCESS_WIFI_STATE
            WifiManager wm = (WifiManager) context
                    .getSystemService(Context.WIFI_SERVICE);
            // gets the MAC address
            String mac = wm.getConnectionInfo().getMacAddress();
            // if we've got nothing, return a random UUID
            if (isEmpty(androidId) && isEmpty(mac)) {
                return UUID.randomUUID();
            }
            // concatenate the string
            String fullHash = buildParams + androidId + mac;
            return UUID.nameUUIDFromBytes(fullHash.getBytes());
        }
    
        public UUID getDeviceUuid() {
            return uuid;
        }
    
        private static boolean isEmpty(Object obj) {
            if (obj == null) {
                return true;
            }
            if ((obj instanceof String) && (((String) obj).trim().length() == 0)) {
                return true;
            }
            if (obj instanceof Map) {
                return ((Map<?, ?>) obj).isEmpty();
            }
            return false;
        }
    }
    

    使用方法()

            DeviceUuidFactory uuidFactory = new DeviceUuidFactory(getApplicationContext());
            String uuid = uuidFactory.getDeviceUuid().toString();
    
    

    相关文章

      网友评论

          本文标题:Android 创建唯一标识UUID

          本文链接:https://www.haomeiwen.com/subject/tyvwodtx.html