android 中如何获取IMEI号

1)在Telephony Framework内部,可以直接使用GSMPhone或GeminiPhone提供的接口。
KK之前的版本:
IMEI(International Mobile Equipment Identity)是国际移动设备身份码,在单卡工程中一个手机对应一个IMEI号,双卡工程中一张卡对应一个IMEI号。
下面是获得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);
 
KK版本上:
GSMPhone.java    中getDeviceId()
GeminiPhone.java 其中getDeviceIdGemini()已经没有了,而getDeviceId()获取的是default phone的IMEI;
所以直接使用GSMPhone.java中getDeviceId()方法; 
Demo code:
GeminiPhone mGeminiPhone;
String imei_sim1=mGeminiPhone.getPhonebyId(PhoneConstants.GEMINI_SIM_1).getDeviceId();
String imei_sim2=mGeminiPhone.getPhonebyId(PhoneConstants.GEMINI_SIM_2).getDeviceId();
 
2)如需要在SDK开发(第三方APK)中获取IMEI号,请使用TelephonyManager中的getDeviceId()方法,或TelephonyManagerEx(双卡接口)中的getDeviceId(int simId)方法。
Demo code(获取DefaultPhone的IMEI):
import android.telephony.TelephonyManager;
String imei_sim = TelephonyManager.getDeviceId();
 
Demo code(双卡接口):
import android.telephony.TelephonyManagerEx;
String imei_sim1 = TelephonyManagerEx.getDeviceId(PhoneConstants.GEMINI_SIM_1);
String imei_sim2 = TelephonyManagerEx.getDeviceId(PhoneConstants.GEMINI_SIM_2);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android获取IMEI码可以通过使用TelephonyManager类的功能来实现。下面是一个示例代码,可以用于获取IMEI码: 1. 首先,在AndroidManifest.xml文件,确保添加了以下许可声明: <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 2. 在你的Activity或Fragment,可以使用以下代码获取IMEI码: // 导入所需的包 import android.Manifest; import android.content.Context; import android.content.pm.PackageManager; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.telephony.TelephonyManager; // 在合适的方法获取IMEI码 public String getImeiNumber(Context context) { if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_PHONE_STATE}, 1); return null; } TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); if (telephonyManager != null) { return telephonyManager.getImei(); } return null; } 3. 你可以在任何需要获取IMEI码的地方调用getImeiNumber()方法。请确保在使用之前检查是否授予了READ_PHONE_STATE权限,如果没有,则应该请求权限。 需要注意的是,为了获取IMEI码,你必须在AndroidManifest.xml文件声明READ_PHONE_STATE权限,并且在运行时进行权限检查。此外,值得注意的是,有些设备可能无法提供IMEI码,而会返回null或其他唯一标识符。所以在使用IMEI码之前应该对返回结果进行适当的判空处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值