Android應(yīng)用中獲取設(shè)備唯一ID的方法有哪些

本篇文章為大家展示了Android應(yīng)用中獲取設(shè)備唯一ID的方法有哪些,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

為正鑲白等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及正鑲白網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、正鑲白網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!

先來看看幾種比較單一的方式:

IMEI

方式:TelephonyManager.getDeviceId():

問題

  1. 范圍:只能支持擁有通話功能的設(shè)備,對(duì)于平板不可以。
  2. 持久性:返廠,數(shù)據(jù)擦除的時(shí)候不徹底,保留了原來的標(biāo)識(shí)。
  3. 權(quán)限:需要權(quán)限:Android.permission.READ_PHONE_STATE
  4. bug: 有些廠家的實(shí)現(xiàn)有bug,返回一些不可用的數(shù)據(jù)

 Mac地址

ACCESS_WIFI_STATE權(quán)限

有些設(shè)備沒有WiFi,或者藍(lán)牙,就不可以,如果WiFi沒有打開,硬件也不會(huì)返回Mac地址,不建議使用

ANDROID_ID
2.2(Froyo,8)版本系統(tǒng)會(huì)不可信,來自主要生產(chǎn)廠商的主流手機(jī),至少有一個(gè)普遍發(fā)現(xiàn)的bug,這些有問題的手機(jī)相同的ANDROID_ID: 9774d56d682e549c

但是如果返廠的手機(jī),或者被root的手機(jī),可能會(huì)變

Serial Number

從Android 2.3 (“Gingerbread”)開始可用,可以通過android.os.Build.SERIAL獲取,對(duì)于沒有通話功能的設(shè)備,它會(huì)返回一個(gè)唯一的device ID,

以下幾個(gè)是stackoverflow上評(píng)論較多的幾個(gè),沒貼完,還有其他,綜合的,用到以上的部分方式:

地址:http://stackoverflow.com/questions/2785485/is-there-a-unique-android-device-id

有興趣的朋友可以再仔細(xì)看看

支持率比較高的(支持票數(shù)157):androidID --> 剔除2.2版本(API 8)中有問題的手機(jī),使用UUID替代

import android.content.Context; 
import android.content.SharedPreferences; 
import android.provider.Settings.Secure; 
import android.telephony.TelephonyManager; 
 
import java.io.UnsupportedEncodingException; 
import java.util.UUID; 
 
public class DeviceUuidFactory { 
 
  protected static final String PREFS_FILE = "device_id.xml"; 
  protected static final String PREFS_DEVICE_ID = "device_id"; 
  protected static volatile UUID uuid; 
 
  public 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); 
          } else { 
            final String androidId = Secure.getString( 
              context.getContentResolver(), 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 { 
                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); 
            } 
            // Write the value out to the prefs file 
            prefs.edit() 
                .putString(PREFS_DEVICE_ID, uuid.toString()) 
                .commit(); 
          } 
        } 
      } 
    } 
  } 
 
  /** 
   * Returns a unique UUID for the current android device. As with all UUIDs, 
   * this unique ID is "very highly likely" to be unique across all Android 
   * devices. Much more so than ANDROID_ID is. 
   * 
   * The UUID is generated by using ANDROID_ID as the base key if appropriate, 
   * falling back on TelephonyManager.getDeviceID() if ANDROID_ID is known to 
   * be incorrect, and finally falling back on a random UUID that's persisted 
   * to SharedPreferences if getDeviceID() does not return a usable value. 
   * 
   * In some rare circumstances, this ID may change. In particular, if the 
   * device is factory reset a new device ID may be generated. In addition, if 
   * a user upgrades their phone from certain buggy implementations of Android 
   * 2.2 to a newer, non-buggy version of Android, the device ID may change. 
   * Or, if a user uninstalls your app on a device that has neither a proper 
   * Android ID nor a Device ID, this ID may change on reinstallation. 
   * 
   * Note that if the code falls back on using TelephonyManager.getDeviceId(), 
   * the resulting ID will NOT change after a factory reset. Something to be 
   * aware of. 
   * 
   * Works around a bug in Android 2.2 for many devices when using ANDROID_ID 
   * directly. 
   * 
   * @see http://code.google.com/p/android/issues/detail?id=10603 
   * 
   * @return a UUID that may be used to uniquely identify your device for most 
   *     purposes. 
   */ 
  public UUID getDeviceUuid() { 
    return uuid; 
  } 
} 

根據(jù)版本進(jìn)行判斷的方式:Serial序列號(hào)-->UUID (支持?jǐn)?shù)31)

通過Serial 即可,在覆蓋率上,你已經(jīng)成功的獲得了98.4%的用戶,剩下的1.6%的用戶系統(tǒng)是在9 以下的。

通過AndroidID獲取,前面已經(jīng)說過,在8上,有些商家的手機(jī)會(huì)有一些bug,返回相同的AndroidID,如果Serial和AndroidID都不行

/** 
 * Return pseudo unique ID 
 * @return ID 
 */ 
public static String getUniquePsuedoID() 
{ 
  // If all else fails, if the user does have lower than API 9 (lower 
  // than Gingerbread), has reset their phone or 'Secure.ANDROID_ID' 
  // returns 'null', then simply the ID returned will be solely based 
  // off their Android device information. This is where the collisions 
  // can happen. 
  // Thanks http://www.pocketmagic.net/?p=1662! 
  // Try not to use DISPLAY, HOST or ID - these items could change. 
  // If there are collisions, there will be overlapping data 
  String m_szDevIDShort = "35" + (Build.BOARD.length() % 10) + (Build.BRAND.length() % 10) + (Build.CPU_ABI.length() % 10) + (Build.DEVICE.length() % 10) + (Build.MANUFACTURER.length() % 10) + (Build.MODEL.length() % 10) + (Build.PRODUCT.length() % 10); 
 
  // Thanks to @Roman SL! 
  // http://stackoverflow.com/a/4789483/950427 
  // Only devices with API >= 9 have android.os.Build.SERIAL 
  // http://developer.android.com/reference/android/os/Build.html#SERIAL 
  // If a user upgrades software or roots their phone, there will be a duplicate entry 
  String serial = null; 
  try 
  { 
    serial = android.os.Build.class.getField("SERIAL").get(null).toString(); 
 
    // Go ahead and return the serial for api => 9 
    return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); 
  } 
  catch (Exception e) 
  { 
    // String needs to be initialized 
    serial = "serial"; // some value 
  } 
 
  // Thanks @Joe! 
  // http://stackoverflow.com/a/2853253/950427 
  // Finally, combine the values we have found by using the UUID class to create a unique identifier 
  return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString(); 
} 

不用READ_PHONE_STATE權(quán)限直接獲取ROM信息的方式:(支持率較低 16)

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 

最后貼上自己在項(xiàng)目中用的:

public static String getDeviceId(Context context) { 
    String deviceId = ""; 
    if (deviceId != null && !"".equals(deviceId)) { 
     return deviceId; 
   } 
    if (deviceId == null || "".equals(deviceId)) { 
      try { 
        deviceId = getLocalMac(context).replace(":", ""); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
    if (deviceId == null || "".equals(deviceId)) { 
      try { 
        deviceId = getAndroidId(context); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
    if (deviceId == null || "".equals(deviceId)) { 
       
      if (deviceId == null || "".equals(deviceId)) { 
        UUID uuid = UUID.randomUUID(); 
        deviceId = uuid.toString().replace("-", ""); 
        writeDeviceID(deviceId); 
      } 
    } 
    return deviceId; 
  } 
// IMEI碼 
  private static String getIMIEStatus(Context context) { 
    TelephonyManager tm = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 
    String deviceId = tm.getDeviceId(); 
    return deviceId; 
  } 
 
  // Mac地址 
  private static String getLocalMac(Context context) { 
    WifiManager wifi = (WifiManager) context 
        .getSystemService(Context.WIFI_SERVICE); 
    WifiInfo info = wifi.getConnectionInfo(); 
    return info.getMacAddress(); 
  } 
 
  // Android Id 
  private static String getAndroidId(Context context) { 
    String androidId = Settings.Secure.getString( 
        context.getContentResolver(), Settings.Secure.ANDROID_ID); 
    return androidId; 
  } 
 
  public static void saveDeviceID(String str) { 
    try { 
      FileOutputStream fos = new FileOutputStream(file); 
      Writer out = new OutputStreamWriter(fos, "UTF-8"); 
      out.write(str); 
      out.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
  } 
 
  public static String readDeviceID() { 
    StringBuffer buffer = new StringBuffer(); 
    try { 
      FileInputStream fis = new FileInputStream(file); 
      InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); 
      Reader in = new BufferedReader(isr); 
      int i; 
      while ((i = in.read()) > -1) { 
        buffer.append((char) i); 
      } 
      in.close(); 
      return buffer.toString(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
    } 
  } 

上述內(nèi)容就是Android應(yīng)用中獲取設(shè)備唯一ID的方法有哪些,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞名稱:Android應(yīng)用中獲取設(shè)備唯一ID的方法有哪些
網(wǎng)站URL:http://muchs.cn/article48/pidgep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、做網(wǎng)站響應(yīng)式網(wǎng)站、網(wǎng)站制作、小程序開發(fā)、網(wǎng)站設(shè)計(jì)公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作