Android Smart Phone IMEI、IMSI简介
android公开的API提供了访问方法,大家都知道使用TelephonyManager提供的方法,但是有些理解有误,如下国内一个比较大的andorid论坛提供的例子,就出现了错误:
帖子如下http://www.eoeandroid.com/thread-14027-1-3.html,其中实现代码没有注释,只能按照变量定义判断:
- TelephonyManagertm=(TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE);
- Stringdeviceid=tm.getDeviceId();
- Stringtel=tm.getLine1Number();//取出用户手机号码,我加的
- Stringimei=tm.getSimSerialNumber();//取出IMEI,我加的
- Stringimsi=tm.getSubscriberId();//取出IMSI,我加的
那么上述出现错误了:String imei =tm.getSimSerialNumber(); //取出IMEI
IMEI是手机的序列号,怎么会通过getSimSerialNumber()方法获得,那么查一下andorid源码可以看出:
http://www.netmite.com/android/mydroid/frameworks/base/telephony/java/android/telephony/TelephonyManager.java
从注释里明显看出来这个函数是取SIM卡序列号的,也就是ICCID的,他用错了。
- /**
- *ReturnstheserialnumberoftheSIM,ifapplicable.
- *<p>
- *RequiresPermission:
- *{@linkandroid.Manifest.permission#READ_PHONE_STATEREAD_PHONE_STATE}
- */
- public StringgetSimSerialNumber(){
- try {
- return getSubscriberInfo().getSimSerialNumber();
- }catch (RemoteExceptionex){
- }
- return null ;
- }
2.相关几个定义、说明:
我们说到的和手机、卡相关的号码数据包括IMSI,MSISDN,ICCID,IMEI
IMSI:international mobiles subscriber identity国际移动用户号码标识,这个一般大家是不知道,GSM必须写在卡内相关文件中;
MSISDN:mobile subscriber ISDN用户号码,这个是我们说的139,136那个号码;
ICCID:ICC identity集成电路卡标识,这个是唯一标识一张卡片物理号码的;
IMEI:international mobile Equipment identity手机唯一标识码;
3.那好我们看看andorid实现TelephonyManager.java的源码:
getDeviceId()取IMEI号没有争议了。
- /**
- *ReturnstheuniquedeviceID,forexample,theIMEIforGSM
- *phones.
- *
- *<p>RequiresPermission:
- *{@linkandroid.Manifest.permission#READ_PHONE_STATEREAD_PHONE_STATE}
- */
- public StringgetDeviceId(){
- try {
- return getSubscriberInfo().getDeviceId();
- }catch (RemoteExceptionex){
- }
- return null ;
- }
getLine1Number()取MSISDN,这个需要说明两点
1) 为什么这个函数叫getLine1Number(),因为andorid实现的时候应该分为GSM和CDMA的,GSM手机使用这个函数,CDMA应该还会由其它实现的。
2) 取MSISDN具体的方法就会导致最后能否取到了,函数中调用了getSubscriberInfo().getLine1Number()去实现,我们下面找找看。
Java代码- /**
- *Returnsthephonenumberstringforline1,forexample,theMSISDN
- *foraGSMphone.
- *<p>
- *RequiresPermission:
- *{@linkandroid.Manifest.permission#READ_PHONE_STATEREAD_PHONE_STATE}
- */
- public StringgetLine1Number(){
- try {
- return getSubscriberInfo().getLine1Number();
- }catch (RemoteExceptionex){
- }
- return null ;
- }
- //找到了
- private IPhoneSubInfogetSubscriberInfo(){
- //getiteachtimebecausethatprocesscrashesalot
- return IPhoneSubInfo.Stub.asInterface(ServiceManager.getService( "iphonesubinfo" ));
- }
- //一个接口,再找有一个PhoneSubInfo.java:
- /**
- *RetrievestheuniquedeviceID,e.g.,IMEIforGSMphonesandMEIDforCDMAphones.
- */
- public StringgetDeviceId(){
- mContext.enforceCallingOrSelfPermission(READ_PHONE_STATE,"RequiresREAD_PHONE_STATE" );
- return mPhone.getDeviceId();
- }
- //前面定义了PhonemPhone,再找Phone.java:
- /**
- *RetrievestheuniquesbuscriberID,e.g.,IMSIforGSMphones.
- */
- StringgetSubscriberId();
- //原来是个接口,发现PhoneProxy.java有具体实现
- public StringgetSubscriberId(){
- return mActivePhone.getSubscriberId();
- }
- //这个mActivePhone是phone的实例,我疯了,于是发现GSMPHONE。java中有了具体实现:
- public StringgetSubscriberId(){
- return mSIMRecords.imsi;
- }
- public StringgetIccSerialNumber(){
- return mSIMRecords.iccid;
- }
- public StringgetLine1Number()
- {
- return mSIMRecords.getMsisdnNumber();
- }
从上面看出来,应该是通过SIM卡相关文件记录得到的上述数据,从其中看到:
public void handleMessage(Message msg) 这个函数进行了真正的处理,重点看:
- case EVENT_GET_MSISDN_DONE:
- isRecordLoadResponse=true ;
- ar=(AsyncResult)msg.obj;
- if (ar.exception!= null ){
- Log.d(LOG_TAG,"InvalidormissingEF[MSISDN]" ); //应该是从sim卡的EFmsisdn文件中取出来的
- break ;
- }
- adn=(AdnRecord)ar.result;
- msisdn=adn.getNumber();
- msisdnTag=adn.getAlphaTag();
- Log.d(LOG_TAG,"MSISDN:" +msisdn);
- break ;
下面的细节就不分析了,那个问题就归结到是否可以从SIM卡的EFmsisdn文件取出手机号码了,不幸的是一般运营商不会把用户号码写在这个文件的,为什么呢?
因为这个手机号码是在用户买到卡并开通时才将IMSI和MSISDN对应上的,卡内生产出来时只有IMSI,你不知道用户喜欢那个手机号码,因此一般不先对应IMSI和MSISDN,即时有对应也不写这个文件的。
4 总结
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId(); //取出IMEI
String tel = tm.getLine1Number(); //取出MSISDN,很可能为空
String imei =tm.getSimSerialNumber(); //取出ICCID
String imsi =tm.getSubscriberId(); //取出IMSI
更多相关文章
- Android 的 dex2jar 和 jd-gui 反编译 apk 源代码
- Android Menu详解及示例代码
- 在android中利用多线程实现对控件的更新(动态修改文本框中的值)。
- 用HTML+JS实现Android闹钟功能,附带Alarm代码分享
- Android JUnit Test——批量运行测试代码
- 在 Android 使用 Support Annotations 注解优化代码
- Android判断网络连接是否可用(代码)
- Android中使用代码开关GPS、移动网络(GPRS)、WiFi