如何獲取Android手機的唯一標識


有很多場景和需求你需要用到手機設備的唯一標識符。

Android中,有以下幾種方法獲取這樣的ID。

1. The IMEI: 僅僅只對Android手機有效:

1
2
TelephonyManager TelephonyMgr = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); String szImei = TelephonyMgr.getDeviceId(); 

采用此種方法,需要在AndroidManifest.xml中加入一個許可:android.permission.READ_PHONE_STATE,並且用戶應當允許安裝此應用。作為手機來講,IMEI是唯一的,它應該類似於 359881030314356(除非你有一個沒有量產的手機(水貨)它可能有無效的IMEI,如:0000000000000)。

2. Pseudo-Unique ID, 這個在任何Android手機中都有效
有一些特殊的情況,一些如平板電腦的設置沒有通話功能,或者你不願加入READ_PHONE_STATE許可。而你仍然想獲得唯一序列號之類的東西。這時你可以通過取出ROM版本、制造商、CPU型號、以及其他硬件信息來實現這一點。這樣計算出來的ID不是唯一的(因為如果兩個手機應用了同樣的硬件以及Rom 鏡像)。但應當明白的是,出現類似情況的可能性基本可以忽略。要實現這一點,你可以使用Build類:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
String m_szDevIDShort = "35" + //we make this look like a valid IMEI Build.BOARD.length()%10 + Build.BRAND.length()%10 + Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + Build.DISPLAY.length()%10 + Build.HOST.length()%10 + Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + Build.TAGS.length()%10 + Build.TYPE.length()%10 + Build.USER.length()%10 ; //13 digits 

大多數的Build成員都是字符串形式的,我們只取他們的長度信息。我們取到13個數字,並在前面加上“35”。這樣這個ID看起來就和15位IMEI一樣了。

3. The Android ID
通常被認為不可信,因為它有時為null。開發文檔中說明了:這個ID會改變如果進行了出廠設置。並且,如果某個Andorid手機被Root過的話,這個ID也可以被任意改變。

1
String m_szAndroidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID); 

Returns: 9774d56d682e549c . 無需任何許可。

4. The WLAN MAC Address string
是另一個唯一ID。但是你需要為你的工程加入android.permission.ACCESS_WIFI_STATE 權限,否則這個地址會為null。

1
2
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); String m_szWLANMAC = wm.getConnectionInfo().getMacAddress(); 

Returns: 00:11:22:33:44:55 (這不是一個真實的地址。而且這個地址能輕易地被偽造。).WLan不必打開,就可讀取些值。

5. The BT MAC Address string
只在有藍牙的設備上運行。並且要加入android.permission.BLUETOOTH 權限.

1
2
3
BluetoothAdapter m_BluetoothAdapter = null; // Local Bluetooth adapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); String m_szBTMAC = m_BluetoothAdapter.getAddress(); 

Returns: 43:25:78:50:93:38 . 藍牙沒有必要打開,也能讀取。

Combined Device ID
綜上所述,我們一共有五種方式取得設備的唯一標識。它們中的一些可能會返回null,或者由於硬件缺失、權限問題等獲取失敗。
但你總能獲得至少一個能用。所以,最好的方法就是通過拼接,或者拼接后的計算出的MD5值來產生一個結果。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
String m_szLongID = m_szImei + m_szDevIDShort + m_szAndroidID+ m_szWLANMAC + m_szBTMAC; // compute md5 MessageDigest m = null; try { m = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } m.update(m_szLongID.getBytes(),0,m_szLongID.length()); // get md5 bytes byte p_md5Data[] = m.digest(); // create a hex string String m_szUniqueID = new String(); for (int i=0;i<p_md5Data.length;i++) { int b = (0xFF & p_md5Data[i]); // if it is a single digit, make sure it have 0 in front (proper padding) if (b <= 0xF) m_szUniqueID+="0"; // add number to string m_szUniqueID+=Integer.toHexString(b); } // hex string to uppercase m_szUniqueID = m_szUniqueID.toUpperCase(); 

通過以上算法,可產生32位的16進制數據:
9DDDF85AFF0A87974CE4541BD94D5F55
現在你就可以對其進行你的應用了。

 

 

 

 

//---------------------------------------------------------------------

android下獲取設備唯一標識原本非常簡單(至少不會像iOS一樣禁用這個,禁用那個),但是由於設備的多樣性需要考慮的東西也對應復雜起來。
  先附上完整代碼
  java代碼

  1.     protected static final String PREFS_FILE = 
  2. "gank_device_id.xml";
  3.   protected static final String PREFS_DEVICE_ID = "gank_device_id";
  4.   protected static String uuid;
  5.   static public String getUDID()
  6.   {
  7.   if( uuid ==null ) {
  8.   synchronized (GankMainActivity.class) {
  9.   if( uuid == null) {
  10.   final SharedPreferences prefs = s_instance.getSharedPreferences( 
  11. PREFS_FILE, 0);
  12.   final String id = prefs.getString(PREFS_DEVICE_ID, null );
  13.   if (id != null) {
  14.   // Use the ids previously computed and stored in the prefs file
  15.   uuid = id;
  16.   } else {
  17.   final String androidId = Secure.getString(s_instance.getContentResolver(), 
  18. Secure.ANDROID_ID);
  19.   // Use the Android ID unless it's broken, in which case fallback on 
  20. deviceId,
  21.   // unless it's not available, then fallback on a random number which we 
  22. store
  23.   // to a prefs file
  24.   try {
  25.   if (!"9774d56d682e549c".equals(androidId)) {
  26.   uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8")).toString();
  27.   } else {
  28.   final String deviceId = ((TelephonyManager) s_instance.getSystemService( 
  29. Context.TELEPHONY_SERVICE )).getDeviceId();
  30.   uuid = deviceId!=null ? 
  31. UUID.nameUUIDFromBytes(deviceId.getBytes("utf8")).toString() : 
  32. UUID.randomUUID().toString();
  33.   }
  34.   } catch (UnsupportedEncodingException e) {
  35.   throw new RuntimeException(e);
  36.   }
  37.   // Write the value out to the prefs file
  38.   prefs.edit().putString(PREFS_DEVICE_ID, uuid).commit();
  39.   }
  40.   }
  41.   }
  42.   }
  43.   return uuid;
  44.   }
復制代碼


  1、正常情況下可以通過((TelephonyManager) s_instance.getSystemService( Context.TELEPHONY_SERVICE )).getDeviceId(); 來獲取,但是某些平板電腦此函數會返回空
  2、通過 Secure.getString(s_instance.getContentResolver(), Secure.ANDROID_ID); 也可以獲取到一個id,但是android2.2或者是某些山寨手機使用這個也是有問題的,它會返回一個固定的值 9774d56d682e549c
  3、如果前兩個都沒有獲取到udid,那么就在程序啟動的時候創建一個隨機的uuid,然后保存起來。這個算是兼容方案,當然這樣的設備並不會很多。


  原文鏈接:http://www.eyeandroid.com/thread-15908-1-1.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM