【工具类】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();
                   }
               }
            }
        }
     }
   }

 

posted on 2013-03-21 12:02  大米稀饭  阅读(1194)  评论(0编辑  收藏  举报