话不多说,直接上代码。这里写的都是Android10以后的方法
//初始化
private TelephonyManager telephonyMng;
telephonyMng = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//由于Android10以后无法根据卡槽获取,这里可以利用反射的方式获取
//type : 0 (卡1),1(卡2)
public String getIMSI(int type) {
try {
String imsi = getImsi(telephonyMng, type);
Log.d("sim", "getIMSI: " + type + ": " + imsi);
return imsi;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
// 反射实现
@SuppressLint("SoonBlockedPrivateApi")
String getImsi(TelephonyManager telMgr, int type) throws Exception {
Method getImsiMethod;
getImsiMethod = telMgr.getClass().getDeclaredMethod("getSubscriberId", int.class);
return (String) getImsiMethod.invoke(telMgr, type);
}
/**
* 获取IMEI号
* type : 0 (卡1),1(卡2)
* @return
*/
@SuppressWarnings("depr