zoukankan      html  css  js  c++  java
  • 【工具类】Android获取平板物理唯一标识码

    import java.io.UnsupportedEncodingException;
    import java.util.UUID;
    import com.vtion.ym.util.Constant;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.provider.Settings.Secure;
    import android.telephony.TelephonyManager;
    
    
    public class DeviceUuidFactory {
        public static final String PREFS_FILE = "device_id.xml";
        public static final String PREFS_DEVICE_ID = "device_id";
        public static UUID uuid;
    
       public static void DeviceUuidFactory(Context context) {
           if( uuid ==null ) {
                synchronized (DeviceUuidFactory.class) {
                    if( uuid == null) {
                        final SharedPreferences prefs = context.getSharedPreferences( PREFS_FILE, 0);
                        final String id = prefs.getString(PREFS_DEVICE_ID, null );
                       if (id != null) {
                            // Use the ids previously computed and stored in the prefs file
                            uuid = UUID.fromString(id);
                            System.out.println("【设备号测试】uuid@-->" + uuid);
                            Constant.DEVICE_ID = uuid+"";
                       } else {
                           final String androidId = Secure.getString(
                                   context.getContentResolver(), Secure.ANDROID_ID);
                           System.out.println("【设备号测试】androidId-->" + androidId);
    
                            // Use the Android ID unless it's broken, in which case fallback on deviceId,
                            // unless it's not available, then fallback on a random number which we store
                            // to a prefs file
                            try {
                                if (!"9774d56d682e549c".equals(androidId)) {
                                    uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));
                                } else {
                                    final String deviceId = ((TelephonyManager) context.getSystemService( 
                                            Context.TELEPHONY_SERVICE )).getDeviceId();
                                    uuid = deviceId!=null ? UUID.nameUUIDFromBytes(
                                            deviceId.getBytes("utf8")) : UUID.randomUUID();
                                            System.out.println("【设备号测试2】uuid-->" + uuid);
                                }
                            } catch (UnsupportedEncodingException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.println("【设备号测试】uuid.toString()-->" + uuid.toString());
                            Constant.DEVICE_ID = uuid.toString();
                            System.out.println("【设备号测试】Constant.DEVICE_ID-->" + Constant.DEVICE_ID);
                            // Write the value out to the prefs file
                            prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();
                       }
                   }
                }
            }
         }
       }
  • 相关阅读:
    Nhibernate 在一个项目中连接不同的数据库。记录
    sql2005 向2000导入数据。一些注意事项
    iis发布系统问题总结 .....关于handler的重写的应用
    ExecutorService 线程池
    Spring Boot 访问静态资源
    spring boot的核心注解
    日期处理工具类
    Json解析工具类
    (技能篇)Mysql在linux下的全量热备份
    Linux相关命令
  • 原文地址:https://www.cnblogs.com/androidsj/p/2973059.html
Copyright © 2011-2022 走看看