Android中如何获取IMEI号码

来源:互联网 发布:数据存储硬盘选择 编辑:程序博客网 时间:2024/05/01 23:41

 


 首先刚看见这个需求,觉得有病吧。没办法也得解决。以下方法亲测有效,希望能帮助到大家。

 首先我们应该注意的是需要一个权限,要不怎么都不会成功的

<uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

 接着就是代码部分了,在网上找了好多资料都没有生效 亲测下面代码有效

//whx import com.android.internal.telephony.PhoneConstants; import android.telephony.TelephonyManager;

 TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); String imei = telephonyManager.getDeviceId();

这样我们就获取到了单卡的IMEI号码了。

 如果想要获取到双卡的:

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);for (int slot = 0; slot < telephonyManager.getPhoneCount(); slot++) {            String imei = telephonyManager.getDeviceId(slot);}

以上方法就可以让我们获取到IMEI号码了。有木有觉得很棒!

我们在放出来资料,希望能帮助到大家

IMEI号是GSM的概念,CDMA对应的是MEID号。

IMEIInternational Mobile Equipment Identity是国际移动设备身份码,目前GSM/WCDMA/LTE手机终端需要使用IMEI号码。在单卡工程中一个手机对应一个IMEI双卡工程中一张卡对应一个IMEI双卡共有两个IMEI号。

MEID (Mobile Equipment Identifier) 移动设备识别码,是CDMA手机的唯一身份识别码。

 

通过GSMPhone对象来调用getDeviceId()函数,获取到的就是IMEI号。

通过CDMAPhone对象来调用getDeviceId()函数,获取到的就是MEID号。

 

一、如何获取IMEI


L版本非C2K项目上

GSMPhone.java中的getDeviceId()

L上面已经没有GeminiPhone

使用方法如下

Phone mPhone1=PhoneFactory.getPhone(PhoneConstants.SIM_ID_1);

Phone mPhone2=PhoneFactory.getPhone(PhoneConstants.SIM_ID_2);

 

if (mPhone1 != null) {

    String imei_sim1 =  mPhone1.getDeviceId();

}

if (mPhone2 != null) {

    String imei_sim2 =  mPhone2.getDeviceId();

}

 

M0.mp1版本C2K项目、

L版本C2K项目上:

L版本C2K项目上,一张卡同时对应一个CDMAPhone和一个GSMPhone,要获取对应卡的IMEI号,需要先获取到对应的GSMPhone对象,具体可以通过下面的方法来获取:

    SIM1-> CDMAPhone = PhoneFactory.getPhone(0).getNLtePhone()

    SIM1-> GSMPhone = PhoneFactory.getPhone(0).getLtePhone()

    SIM2->  CDMAPhone = PhoneFactory.getPhone(1).getNLtePhone()

    SIM2->  GSMPhone = PhoneFactory.getPhone(1).getLtePhone()

 

获取到GSMPhone对象后,通过该对象来调用getDeviceId()函数。

 

KK版本上:

GSMPhone.java    getDeviceId()

GeminiPhone.java 其中getDeviceIdGemini()已经没有了getDeviceId()获取的是default phoneIMEI

所以直接使用GSMPhone.javagetDeviceId()方法 

Demo code:

GeminiPhone mGeminiPhone;

String imei_sim1=mGeminiPhone.getPhonebyId(PhoneConstants.GEMINI_SIM_1).getDeviceId();

String imei_sim2=mGeminiPhone.getPhonebyId(PhoneConstants.GEMINI_SIM_2).getDeviceId();

 

KK之前的版本:

下面是获得IMEI号的接口和demo code

API

GSMPhone.java     getDeviceId()

GeminiPhone.java  getDeviceId() getDeviceIdGemini()

 

Demo code:

import com.android.internal.telephony.Phone;

import com.android.internal.telephony.gemini.GeminiPhone;

import com.android.internal.telephony.PhoneFactory;  

Phone phone;

phone = PhoneFactory.getDefaultPhone();

String  imei=(GeminiPhone)phone.getDeviceId();

 

GeminiPhone mGeminiPhone;

String imei_sim1 = mGeminiPhone.getDeviceIdGemini(PhoneConstants.GEMINI_SIM_1);

String imei_sim2 = mGeminiPhone.getDeviceIdGemini(PhoneConstants.GEMINI_SIM_2);


0 0