Android获取设备唯一标识完美解决方案

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/LANGZI7758521/article/details/52496180

 /**
     * deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符
     *
     * 渠道标志为:
     * 1,andriod(a)
     *
     * 识别符来源标志:
     * 1, wifi mac地址(wifi);
     * 2, IMEI(imei);
     * 3, 序列号(sn);
     * 4, id:随机码。若前面的都取不到时,则随机生成一个随机码,需要缓存。
     *
     * @param context
     * @return
     */
    public static String getDeviceId(Context context) {

        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        deviceId.append("a");

        try {

            //wifi mac地址
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String wifiMac = info.getMacAddress();
            if(!isEmpty(wifiMac)){
                deviceId.append("wifi");
                deviceId.append(wifiMac);
                PALog.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }

            //IMEI(imei)
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            if(!isEmpty(imei)){
                deviceId.append("imei");
                deviceId.append(imei);
                PALog.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }

            //序列号(sn)
            String sn = tm.getSimSerialNumber();
            if(!isEmpty(sn)){
                deviceId.append("sn");
                deviceId.append(sn);
                PALog.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }

            //如果上面都没有, 则生成一个id:随机码
            String uuid = getUUID(context);
            if(!isEmpty(uuid)){
                deviceId.append("id");
                deviceId.append(uuid);
                PALog.e("getDeviceId : ", deviceId.toString());
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
            deviceId.append("id").append(getUUID(context));
        }

        PALog.e("getDeviceId : ", deviceId.toString());

        return deviceId.toString();

    }


    /**
     * 得到全局唯一UUID
     */
    public static String getUUID(Context context){
        SharedPreferences mShare = getSysShare(context, "sysCacheMap");
        if(mShare != null){
            uuid = mShare.getString("uuid", "");
        }

        if(isEmpty(uuid)){
            uuid = UUID.randomUUID().toString();
            saveSysMap(context, "sysCacheMap", "uuid", uuid);
        }

        PALog.e(tag, "getUUID : " + uuid);
        return uuid;
    } 
亲测:
package org.cocos2dx.javascript;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;

import static org.cocos2dx.lib.Cocos2dxActivity.getContext;

/**
 * Created by Lovell on 9/19/16.
 */
public class UDID {

    public static String UDID() {


        StringBuilder deviceId = new StringBuilder();
        // 渠道标志
        deviceId.append("a");

        try {
            // wifi mac地址
            WifiManager wifi = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String wifiMac = info.getMacAddress();
            if (!wifiMac.isEmpty()) {
                deviceId.append("wifi");
                deviceId.append(wifiMac);
                return deviceId.toString();
            }

            // IMEI(imei)
            TelephonyManager tm = (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            if (imei.isEmpty()) {
                deviceId.append("imei");
                deviceId.append(imei);
                return deviceId.toString();
            }

            // 序列号(sn)
            String sn = tm.getSimSerialNumber();
            if (!sn.isEmpty()) {
                deviceId.append("sn");
                deviceId.append(sn);
                return deviceId.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return deviceId.toString();
    }
}

来自:  http://blog.csdn.net/haoman100/article/details/8729803

展开阅读全文

没有更多推荐了,返回首页