Android如何获取唯一ID
正文
IMEI
概念
摘自維基百科:
國際移動設備識別碼(International Mobile Equipment Identity,IMEI),即通常所說的手機序列號、手機“串號”,用于在行動電話網絡中識別每一部獨立的手機等行動通訊裝置,相當于行動電話的身份證。序列號共有15位數字,前6位(TAC)是型號核準號碼,代表手機類型。接著2位(FAC)是最后裝配號,代表產地。后6位(SNR)是串號,代表生產順序號。最后1位(SP)一般為0,是檢驗碼,備用。國際移動設備識別碼一般貼于機身背面與外包裝上,同時也存在于手機記憶體中,通過輸入*#06#即可查詢。
代碼
獲取權限,修改AndroidManifest.xml。
<uses-permission android:name="android.permission.READ_PHONE_STATE" />樣例代碼,參考這里:
public static String getIMEI(Context context) {String imei = "";try {TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {return imei;}imei = telephonyManager.getDeviceId();if (imei == null && imei.isEmpty()) {return "";}} catch (Exception e) {e.printStackTrace();}return imei; }MAC
概念:
摘自維基百科:
路由器標簽上的MAC地址(LAN/WLAN)
MAC地址(英語:Media Access Control Address),直譯為媒體訪問控制地址,也稱為局域網地址(LAN Address),以太網地址(Ethernet Address)或物理地址(Physical Address),它是一個用來確認網絡設備位置的地址。在OSI模型中,第三層網絡層負責IP地址,第二層數據鏈接層則負責MAC地址。MAC地址用于在網絡中唯一標示一個網卡,一臺設備若有一或多個網卡,則每個網卡都需要并會有一個唯一的MAC地址。
代碼:
獲取權限:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>樣例代碼,參考這里:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wInfo = wifiManager.getConnectionInfo(); String macAddress = wInfo.getMacAddress();SSN(sim卡序列號)
概念:
摘自這里:
Your SIM serial number (SSN), sometimes called the ICC-ID (Integrated Circuit Card ID), is for international identification. The SNN typically has 19 digits and contains specific details about your operator, your location, and when it was made. The first two digits are the telecom ID, the second two digit refer to your country code, the third two digits are the network code, the next four digits are the month and year of manufacturing, the next two digits are the switch configuration code, the next six digits are the SIM number, and the last digit is the check digit.
簡單翻譯:SIM 序列號,有的時候又被成為ICC-ID(集成電路卡ID),典型的SSN是由19位數字組成的,包含一些運行商、位置信息等。代碼:
獲取權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>樣例代碼:
public static String getSimId (Context context) {TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);String SimSerialNumber = tm.getSimSerialNumber();return SimSerialNumber; }IMSI
概念:
摘自這里:
The international mobile subscriber identity or IMSI /??mzi?/ is used to identify the user of a cellular network and is a unique identification associated with all cellular networks. It is stored as a 64 bit field and is sent by the phone to the network. It is also used for acquiring other details of the mobile in the home location register (HLR) or as locally copied in the visitor location register. To prevent eavesdroppers identifying and tracking the subscriber on the radio interface, the IMSI is sent as rarely as possible and a randomly generated TMSI is sent instead.
簡單翻譯:IMSI,國際移動訂戶標識,用于標識移動網絡的用戶,是移動網絡的一個唯一的標識。它保存為一個64位的域,由手機發給網絡。
代碼:
獲取權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>樣例代碼,參考這里:
String IMSI = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI); //這種方法還沒有確認過,看帖子上說,這個代碼google沒有暴露出來,需要通過一些技巧去使用另外一個樣例:
private static String getIMSI(Context context) {String imsi = "";try {TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {return imsi;}imsi = telephonyManager.getSubscriberId();if (TextUtils.isEmpty(imsi) || IMSI == imsi) {return imsi;}return imsi;} catch (Exception e) {e.printStackTrace();}return imsi;}ANDROIDID - 安卓ID
概念:
參考這里:
On Android 8.0 (API level 26) and higher versions of the platform, a 64-bit number (expressed as a hexadecimal string), unique to each combination of app-signing key, user, and device. Values of ANDROID_ID are scoped by signing key and user. The value may change if a factory reset is performed on the device or if an APK signing key changes. For more information about how the platform handles ANDROID_ID in Android 8.0 (API level 26) and higher, see Android 8.0
簡單翻譯:Android8和更過版本,提供了一個64位的標識符,對于app簽名的key、用戶和設備是唯一的。
代碼:
獲取權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />樣例代碼:
public static String getAndroidId (Context context) {String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);return androidId; }Serial Number
概念
參考這里:
A hardware serial number, if available. Alphanumeric only, case-insensitive. For apps targeting SDK higher than Build.VERSION_CODES.O_MR1 this field is set to UNKNOWN.
簡單翻譯:硬件序列號。
代碼:
獲取權限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />樣例代碼
public static String getSerialNumber (Context context) {String SerialNumber = android.os.Build.getSerial();return SerialNumber; }參考
https://blog.csdn.net/jiangtea/article/details/72889018,這個帖子整理得挺好。
https://blog.csdn.net/aa1733519509/article/details/50053553,這里面有一些樣例代碼。
總結
以上是生活随笔為你收集整理的Android如何获取唯一ID的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA NIO 实现群聊
- 下一篇: 小球碰撞python代码_Java 实现