Android getDeviceId
获取android设备的IMEI号不是一件难事,方法大家都能百度出来,如下面:
TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
manager.getDeviceId()
上面的写法是对于android sdk level低于23是适用的,但是对于sdk level大于或者等于23的系统来说,这样的写法有时候不适用,获取到的IMEI号可能为空,这时并不表示这个设备没有IMEI号,我们换个方法就可能正确得到IMEI号。其实android sdk不低于23的API提供了一个带参数的getDeviceId方法,参数是卡槽号(0或者1),所以对于android 6.0及以上的系统,正确获取IMEi的写法应该是下面这种形式:
try {
final TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getDeviceId() == null || manager.getDeviceId().equals("")) {
if (Build.VERSION.SDK_INT >= 23) {
tac = manager.getDeviceId(0);
}
}else{
tac = manager.getDeviceId());
}
}catch (Exception e)
{
}