Android UUID生成(IMEI、设备序列号、MAC地址)

根据手机设备的IMEI、设备序列号、MAC地址经过MD5加密,得到手机专有的序号;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;

public class Utils
{

    public Utils()
    {
    }

    public static final String generateUUID(Context context)
    {
        String s = ((TelephonyManager)context.getSystemService("phone")).getDeviceId();
        if(s == null)
            s = "";
        String s1 = android.provider.Settings.Secure.getString(context.getContentResolver(), "android_id");
        if(s1 == null)
            s1 = "";
        String s2;
        String s3;
        WifiInfo wifiinfo;
        String s4;
        if(android.os.Build.VERSION.SDK_INT >= 9)
        {
            s2 = Build.SERIAL;
            if(s2 == null)
                s2 = "";
        } else
        {
            s2 = getDeviceSerial();
        }
        s3 = "";
        wifiinfo = ((WifiManager)context.getSystemService("wifi")).getConnectionInfo();
        if(wifiinfo != null)
        {
            s3 = wifiinfo.getMacAddress();
            if(s3 == null)
                s3 = "";
        }
        try
        {
            s4 = getMD5String((new StringBuilder()).append(s).append(s1).append(s2).append(s3).toString());
        }
        catch(NoSuchAlgorithmException nosuchalgorithmexception)
        {
            nosuchalgorithmexception.printStackTrace();
            return null;
        }
        return s4;
    }

    private static final String getDeviceSerial()
    {
        String s;
        try
        {
            Method method = Class.forName("android.os.Build").getDeclaredMethod("getString", new Class[] {
                Class.forName("java.lang.String")
            });
            if(!method.isAccessible())
                method.setAccessible(true);
            s = (String)method.invoke(new Build(), new Object[] {
                "ro.serialno"
            });
        }
        catch(ClassNotFoundException classnotfoundexception)
        {
            classnotfoundexception.printStackTrace();
            return "";
        }
        catch(NoSuchMethodException nosuchmethodexception)
        {
            nosuchmethodexception.printStackTrace();
            return "";
        }
        catch(InvocationTargetException invocationtargetexception)
        {
            invocationtargetexception.printStackTrace();
            return "";
        }
        catch(IllegalAccessException illegalaccessexception)
        {
            illegalaccessexception.printStackTrace();
            return "";
        }
        return s;
    }

    private static final String getMD5String(String s)
        throws NoSuchAlgorithmException
    {
        byte abyte0[] = MessageDigest.getInstance("SHA-1").digest(s.getBytes());
        Formatter formatter = new Formatter();
        int i = abyte0.length;
        for(int j = 0; j < i; j++)
        {
            byte byte0 = abyte0[j];
            Object aobj[] = new Object[1];
            aobj[0] = Byte.valueOf(byte0);
            formatter.format("%02x", aobj);
        }

        return formatter.toString();
    }
}

本段代码反编译而得

转载于:https://www.cnblogs.com/sishuiliuyun/archive/2013/05/15/3079827.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值