获取客户端唯一标识码

来源:互联网 发布:sql注入检测网站 编辑:程序博客网 时间:2024/05/01 23:53

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