由MCC MNC谈到TelephonyManager

来源:互联网 发布:网络映射在哪里 编辑:程序博客网 时间:2024/04/28 07:30

一切的一切都能在这里找到答案:Android APIs : TelephonyManager

名词解释

MCC

移动国家代码(Mobile Country Code) 3位,我国为460
唯一地识别移动客户所属的国家

MNC

移动网络代码(Mobile Network Code) 2位,移动00&02 联通01 电信03
识别移动客户所归属的移动网

获取方式

通常你会得到这样一段代码:

TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE); String imsi = mTelephonyManager.getSubscriberId();if(!TextUtils.isEmpty(imsi)){        String mcc = imsi.substring(0,3);    String mnc = imsi.substring(3,5);}    

这里你得到的MCC MNC是什么呢,其实是 固化在sim内的IMSI码的前5位,代表的是这张sim卡的所属国和所属运营商。

但,你也可能得到这样一段代码:

TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);String operator = mTelephonyManager.getNetworkOperator();if (!TextUtils.isEmpty(operator)) {    String mcc = operator.substring(0, 3);    String mnc = operator.substring(3, 5);}

这里你得到的MCC MNC是什么呢,其实是 所使用基站信息的前5位,就是手机目前接入的运营商信号塔的信息,代表的是这座基站的所属国和所属运营商。
可以通过MCC判断用户(手机)当前所在的国家。

上面两段代码的差别就是:getSubscriberId 和 getNetworkOperator

getSubscriberId

APIs :

Returns the unique subscriber ID, for example, the IMSI for a GSM phone. Return null if it is unavailable.

返回唯一的用户ID,例如,GSM制式手机的IMSI码。
如果不可用返回null。

Requires Permission: READ_PHONE_STATE

所需权限:READ_PHONE_STATE

上面提到了一个名词 IMSI,我们展开说一下两个名词:IMSI 和 IMEI

IMSI

  1. 描述:
    维基百科

    国际移动用户识别码(International Mobile Subscriber Identity,IMSI)
    用于区分蜂窝网络中不同用户的、在所有蜂窝网络中不重复的识别码。
    手机将IMSI存储于一个64比特的字段发送给网络。IMSI可以用来在归属位置寄存器(HLR,Home Location Register)或拜访位置寄存器(VLR,Visitor Location Register)中查询用户的信息。
    为了避免被监听者识别并追踪特定的用户,大部分情形下手机和网络之间的通信会使用随机产生的临时移动用户识别码(TMSI,Temporary Mobile Subscriber Identity)代替IMSI。

  2. 获取方式:getSubscriberId
    上面已经写到了,就是通过 getSubscriberId

    TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);String imsi = mTelephonyManager.getSubscriberId();

IMEI

  1. 描述:
    维基百科

    国际移动设备识别码(International Mobile Equipment Identity,IMEI)
    即通常所说的手机序列号、手机“串号”,用于在移动电话网络中识别每一部独立的手机等行动通讯装置,相当于移动电话的身份证。
    序列号共有15位数字,前6位(TAC)是型号核准号码,代表手机类型。接着2位(FAC)是最后装配号,代表产地。后6位(SNR)是串号,代表生产顺序号。最后1位(SP)一般为0,是检验码,备用。
    国际移动设备识别码一般贴于机身背面与外包装上,同时也存在于手机内存中,通过输入*#06#即可查询。

  2. 获取方式:getDeviceId

    TelephonyManager mTelephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);String imei = mTelephonyManager.getDeviceId()

    APIs :

    Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones. Return null if device ID is not available.

    返回唯一设备ID,例如, GSM 制式手机的IMEI 和 CDMA制式手机的MEID或ESN。
    如果设备ID不可用的则返回null。

    Requires Permission: READ_PHONE_STATE

    所需权限:READ_PHONE_STATE

getNetworkOperator

APIs :

Returns the numeric name (MCC+MNC) of current registered operator.

返回(用户sim卡)当前所注册的运营商的数字名称(MCC+MNC)。
其实就是指当前用户通讯所链接的基站的信息

Availability: Only when user is registered to a network. Result may be unreliable on CDMA networks (use getPhoneType() to determine if on a CDMA network).

可用性:只有当用户注册到网络时才可用。在CDMA网络中结果可能是不可靠的(在CDMA网络中可以使用getPhoneType()来确定)。

小结

上面描述可知,其实 getNetworkOperator 得到的才能真正的称为 MCC+MNC,而 getSubscriberId 得到的是 IMSI,只是IMSI前5位正好也是由 MCC+MNC 组成的,但,它们是固定的,表示的是sim卡的所属国和运营商。




扩展1:MEID

移动设备识别码(Mobile Equipment Identifier)是CDMA手机的身份识别码,也是每台CDMA手机或通讯平板唯一的识别码。 通过这个识别码,网络端可以对该手机进行跟踪和监管。 用于CDMA制式的手机。 MEID的数字范围是十六进制的,和IMEI的格式类似。

MEID的获取方式和IMEI相同。

扩展2:通过基站信息定位用户(手机)位置
名词解释:

MCC:Mobile Country Code,移动国家代码。
MNC:Mobile Network Code,移动网络号码。
LAC:Location Area Code,位置区域码。
CID:Cell Identity,基站编号。

public static Map<String, Integer> getGSMCellLocation(Context context) {    Map<String, Integer> GSMMap = new HashMap<String, Integer>();    TelephonyManager mTelephonyManager = (TelephonyManager) context.            getSystemService(Context.TELEPHONY_SERVICE);    int mcc = -1;    int mnc = -1;    int lac = -1;    int cid = -1;    if (mTelephonyManager != null) {        CellLocation cellLocation = mTelephonyManager.getCellLocation();        if (cellLocation != null && cellLocation instanceof GsmCellLocation) {            GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;            lac = gsmCellLocation.getLac();            cid = gsmCellLocation.getCid();        } else if (cellLocation != null && cellLocation instanceof CdmaCellLocation) {            CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;            lac = cdmaCellLocation.getNetworkId();            cid = cdmaCellLocation.getBaseStationId();        }        String operator = mTelephonyManager.getNetworkOperator();        if (!TextUtils.isEmpty(operator)) {            mcc = Integer.parseInt(operator.substring(0, 3));            mnc = Integer.parseInt(operator.substring(3, 5));        }    GSMMap.put("mcc", mcc);    GSMMap.put("mnc", mnc);    GSMMap.put("lac", lac);    GSMMap.put("cid", cid);    return GSMMap;}
1 0
原创粉丝点击