加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android获取系统唯一识别码

发布时间:2020-05-24 18:22:24 所属栏目:Java 来源:互联网
导读:Android获取系统唯一识别码

下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。

脚本之家小编现在分享给大家,也给大家做个参考。

public class helper {

    private static String TAG="util.helper";

    private static String SYS_ID = null;

    public static String getUniqDeviceId(Context context) {
        String id = getUniqueID(context);
        if (id == null)
            id = Settings.Secure.getString(context.getContentResolver(),Settings.Secure.ANDROID_ID);
        return id;
    }



    public static String getStringIntegerHexBlocks(int value) {
        String result = "";
        String string = Integer.toHexString(value);

        int remain = 8 - string.length();
        char[] chars = new char[remain];
        Arrays.fill(chars,'0');
        string = new String(chars) + string;

        int count = 0;
        for (int i = string.length() - 1; i >= 0; i--) {
            count++;
            result = string.substring(i,i + 1) + result;
            if (count == 4) {
                result = "-" + result;
                count = 0;
            }
        }

        if (result.startsWith("-")) {
            result = result.substring(1,result.length());
        }

        return result;
    }

    private static String getUniqueID(Context context) {

        if ( SYS_ID != null ) {
            return SYS_ID;
        }

        String telephonyDeviceId;
        String androidDeviceId;

        String error = "0000-0000-0000-0000";

        // get telephony id
        try {
            final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            telephonyDeviceId = tm.getDeviceId();
            if (telephonyDeviceId == null) {
                Log.e(TAG,"Get TelephonyManager DeviceId(IMEI) Error");
                return error;
            }
            Log.e(TAG,"TelephonyManager getDeviceId: " + telephonyDeviceId);
        } catch (Exception e) {
            Log.e(TAG,"Get TelephonyManager DeviceId(IMEI) Error" + e.getMessage());
            return error;
        }

        // get internal android device id
        try {
            androidDeviceId = android.provider.Settings.Secure.getString(context.getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
            if (androidDeviceId == null) {
                Log.e(TAG,"Get androidDeviceId Error");
                return error;
            }
            Log.e(TAG,"android.provider.Settings.Secure.ANDROID_ID : " + androidDeviceId );
        } catch (Exception e) {
            Log.e(TAG,"Get androidDeviceId Error"+e.getMessage());
            return error;
        }

        // build up the uuid
        try {
            String id = getStringIntegerHexBlocks(androidDeviceId.hashCode())
                    + "-"
                    + getStringIntegerHexBlocks(telephonyDeviceId.hashCode());

            SYS_ID = id;
            Log.e(TAG,SYS_ID);
            return SYS_ID;
        } catch (Exception e) {
            return error;
        }
    }
}

来自:http://blog.suchasplus.com/2015/02/android-get-imei-and-ANDROID-ID.html

以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读