1. 程式人生 > >Android獲取唯一識別號的方法

Android獲取唯一識別號的方法

 /**
     * 獲取ip地址
     * @param context
     * @return
     */
    public static String getIpAddress(Context context){
        NetworkInfo info = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        String current_ip = "";
        if (info != null && info.isConnected()) {
            // 3/4g網路
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                try {
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                current_ip = inetAddress.getHostAddress();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }

            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                //  wifi網路
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                current_ip = intIP2StringIP(wifiInfo.getIpAddress());
            }  else if (info.getType() == ConnectivityManager.TYPE_ETHERNET){
                current_ip = getLocalIp();
            }
        }
//        isLocalIp = true;
        return current_ip;
    }



    public static String getUniqueId(Context context){
        String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        String id = androidID + Build.SERIAL;
        try {
            return toMD5(id);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return id;
        }
    }


    private static String toMD5(String text) throws NoSuchAlgorithmException {
        //獲取摘要器 MessageDigest
        MessageDigest messageDigest = MessageDigest.getInstance("MD5");
        //通過摘要器對字串的二進位制位元組陣列進行hash計算
        byte[] digest = messageDigest.digest(text.getBytes());

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digest.length; i++) {
            //迴圈每個字元 將計算結果轉化為正整數;
            int digestInt = digest[i] & 0xff;
            //將10進位制轉化為較短的16進位制
            String hexString = Integer.toHexString(digestInt);
            //轉化結果如果是個位數會省略0,因此判斷並補0
            if (hexString.length() < 2) {
                sb.append(0);
            }
            //將迴圈結果新增到緩衝區
            sb.append(hexString);
        }
        //返回整個結果
        String s = sb.toString();
        if(s.length() >= 10){
            s = s.substring(0,10);
        }
        return s;
    }

    private static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }
 public static String getReplyQueueNameByMAC(Context context, int maxSize) {
        String queueName = "";
        String macAddress = "";
        char c;
        int hexNumber = 0;
         macAddress = getMacAddr();
        String[] afterSplit = macAddress.split(":");
        for (int i = 0; i < afterSplit.length; i++) {
            if (ServiceBean.isIntegerRE(afterSplit[i])) {
                hexNumber = Integer.parseInt(afterSplit[i]);
                if ((hexNumber > 47 && hexNumber < 58)
                        || (hexNumber > 64 && hexNumber < 91)
                        || (hexNumber > 96 && hexNumber < 123)) {
                    c = (char) (Integer.parseInt(afterSplit[i]));
                    queueName += c;
                } else {
                    queueName += String.valueOf(hexNumber);
                }
            } else {
                queueName += afterSplit[i];
            }
        }
        if (queueName.length() > maxSize) {
            queueName = queueName.substring(0,maxSize);
        }
        if(StringUtil.isEmpty(queueName) || "200000".equals(queueName)){
            queueName = NetworkUtils.getUniqueId(context);
        }

        return queueName;
    }
 public static String getMacAddr() {
        try {
            List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface nif : all) {
                if (!nif.getName().equalsIgnoreCase("wlan0")) continue;

                byte[] macBytes = nif.getHardwareAddress();
                if (macBytes == null) {
                    return "";
                }
                StringBuilder res1 = new StringBuilder();
                for (byte b : macBytes) {
                    res1.append(String.format("%02X:",b));
                }

                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception ex) {
        }
        return "02:00:00:00:00:00";
    }



結合了好多人的經驗,因為一直沒找到源頭,座、