获取客户端唯一标识码

在项目中我们通常会用到手机的唯一标识码传给服务器用于统计用户量什么的,我们有可能使用手机的IMEI或者AndroidID、Mac地址等之类的作为标识,但是这些标识对于有的手机可能获取不到导致唯一标识上传失败,这一类的问题我们应该都遇到过,这类问题其实也简单,那就是把IMEI,AndroidID,Mac地址等结合然后通过加密进行上传,三种标识相结合,总有一种会存在,所以不用怕获取不到唯一标识,下面是我在网上看到的一种方法,在这里记录下,也希望能帮到大家:

public static String getDevUUid(){
    Context context = BaseAplication.getInstance();
    String imei = "";
    String androidId = "";
    String macAddress = "";

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(TELEPHONY_SERVICE);
    if (telephonyManager != null) {
        imei = telephonyManager.getDeviceId();
        Log.e("imei---",imei);
    }
    ContentResolver contentResolver = context.getContentResolver();
    if (contentResolver != null) {
        androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID);
        Log.e("androidId---",androidId);
    }
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager != null) {
        macAddress = wifiManager.getConnectionInfo().getMacAddress();
        Log.e("macAddress---",macAddress);
    }

    StringBuilder uidBuilder = new StringBuilder();
    if (imei != null) {
        uidBuilder.append(imei);
    }
    if (androidId != null) {
        uidBuilder.append(androidId);
    }
    if (macAddress != null) {
        uidBuilder.append(macAddress);
    }
    return toMd5(uidBuilder.toString());
}

/**
 * 加密
 * @param plaintext 明文
 * @return ciphertext 密文
 */
public final static String  toMd5(String plaintext) {
    char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f' };
    try {
        byte[] btInput = plaintext.getBytes();
        // 获得MD5摘要算法的 MessageDigest 对象
        MessageDigest mdInst = MessageDigest.getInstance("MD5");
        // 使用指定的字节更新摘要
        mdInst.update(btInput);
        // 获得密文
        byte[] md = mdInst.digest();
        // 把密文转换成十六进制的字符串形式
        int j = md.length;
        char str[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[byte0 >>> 4 & 0xf];
            str[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(str);
    } catch (Exception e) {
        return null;
    }
}


在Java中,你可以通过`java.net.InetAddress`类和`MacAddress`类来获取客户端的机器码或硬件地址(MAC地址)。这里是一个简单的示例: ```java import java.net.InetAddress; import java.net.UnknownHostException; import java.util.UUID; public class Main { public static void main(String[] args) { // 获取本地IP地址 try { InetAddress inetAddress = InetAddress.getLocalHost(); System.out.println("IP Address: " + inetAddress.getHostAddress()); // 获取物理地址(MAC地址) byte[] macBytes = getHardwareAddress(inetAddress); if (macBytes != null) { String macAddress = bytesToHex(macBytes); System.out.println("MAC Address: " + macAddress); } else { System.out.println("Physical address not available."); } } catch (UnknownHostException e) { System.err.println("Failed to get local host information: " + e.getMessage()); } // 使用UUID获取机器唯一标识,通常这不是严格的机器码,但有时作为替代 UUID uuid = UUID.randomUUID(); System.out.println("UUID: " + uuid.toString()); } private static byte[] getHardwareAddress(InetAddress inetAddress) { NetworkInterface networkInterface; try { networkInterface = NetworkInterface.getByInetAddress(inetAddress); if (networkInterface != null) { return networkInterface.getHardwareAddress(); } } catch (SocketException e) { e.printStackTrace(); } return null; } private static String bytesToHex(byte[] bytes) { StringBuilder result = new StringBuilder(); for (byte b : bytes) { result.append(String.format("%02x", b)); } return result.toString().toUpperCase(); } } ``` 在这个例子中,我们首先尝试获取本地主机的IP地址,然后从网络接口获取MAC地址。如果无法获取到MAC地址,程序会打印出相应的提示。最后,我们还展示了如何使用Java的`UUID`生成一个随机的机器唯一标识
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值