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)