问答一下,轻松解决,电脑应用解决专家!
主板显卡CPU内存显示器
硬盘维修显卡维修显示器维修
注册表系统命令DOS命令Win8
存储光存储鼠标键盘
内存维修打印机维修
WinXPWin7Win10/Win11
硬件综合机箱电源散热器手机数码
主板维修CPU维修键盘鼠标维修
Word教程Excel教程PowerPointWPS
网络工具系统工具图像工具
数据库javascriptLinux系统
PHP教程CSS教程XML教程

获取客户端唯一标识码

更新时间:2021-10-28 14:41 作者:Jersey-Guo点击:

在项目中我们通常会用到手机的唯一标识码传给服务器用于统计用户量什么的,我们有可能使用手机的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;
    }
}

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
你可能感兴趣的内容