Android设备获取唯一标识DeviceId

import android.annotation.SuppressLint;import android.content.Context;import android.content.SharedPreferences;import android.provider.Settings;import android.telephony.TelephonyManager;import java.io.UnsupportedEncodingException;import java.util.UUID;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);                         Common.DEVICE_ID = uuid+"";                    } else {                        final String androidId = Settings.Secure.getString(                                context.getContentResolver(), Settings.Secure.ANDROID_ID);                        // 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 {                                @SuppressLint("MissingPermission")                                final String deviceId = ((TelephonyManager) context.getSystemService(                                        Context.TELEPHONY_SERVICE )).getDeviceId();                                uuid = deviceId!=null ? UUID.nameUUIDFromBytes(                                        deviceId.getBytes("utf8")) : UUID.randomUUID();                            }                        } catch (UnsupportedEncodingException e) {                            throw new RuntimeException(e);                        }                         Common.DEVICE_ID = uuid.toString();                        // Write the value out to the prefs file                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString() ).commit();                    }                }            }        }    }

更多相关文章

  1. c语言标识符有哪些
  2. c语言的标识符允许使用关键字吗?
  3. C语言中的标识符由什么组成
  4. c语言标识符有哪三类?
  5. C++语言标识符的命名规则是什么?
  6. c语言用户标识符命名规则是什么?
  7. c++标识符命名规则
  8. c语言中合法标识符有哪些
  9. c语言中标识符不能与保留字同名吗?

随机推荐

  1. Android中Parcelable接口用法
  2. android 混淆
  3. Android 3.0 Platform Highlights
  4. Android进度条实例
  5. Android使用意图链接活动和意图筛选器
  6. Android获取IMEI和ICCID
  7. android美女更衣
  8. Android 震动 和 停止 代码
  9. Android Volley完全解析
  10. (64位)ubuntu 11.10编译android 2.3准备的