<pre name="code" class="html">源地址:http://www.2cto.com/kf/201403/283038.html
由於國內的運營商問題,雙卡手機獲取IMSI號問題要根據廠商API 來實現。
下面我們就來做一套完整的分析運營商獲取IMSI號邏輯。
1,首先我們要判斷手機的平臺。
1.1,判斷手機是否MTK平臺
<p>
</p>public static MtkDoubleInfo initMtkDoubleSim(Context mContext) {
MtkDoubleInfo mtkDoubleInfo = new MtkDoubleInfo();
try {
TelephonyManager tm = (TelephonyManager) mContext
.getSystemService(Context.TELEPHONY_SERVICE);
Class<!--?--> c = Class.forName("com.android.internal.telephony.Phone");
Field fields1 = c.getField("GEMINI_SIM_1");
fields1.setAccessible(true);
mtkDoubleInfo.setSimId_1((Integer) fields1.get(null));
Field fields2 = c.getField("GEMINI_SIM_2");
fields2.setAccessible(true);
mtkDoubleInfo.setSimId_2((Integer) fields2.get(null));
Method m = TelephonyManager.class.getDeclaredMethod(
"getSubscriberIdGemini", int.class);
mtkDoubleInfo.setImsi_1((String) m.invoke(tm,
mtkDoubleInfo.getSimId_1()));
mtkDoubleInfo.setImsi_2((String) m.invoke(tm,
mtkDoubleInfo.getSimId_2()));
Method m1 = TelephonyManager.class.getDeclaredMethod(
"getDeviceIdGemini", int.class);
mtkDoubleInfo.setImei_1((String) m1.invoke(tm,
mtkDoubleInfo.getSimId_1()));
mtkDoubleInfo.setImei_2((String) m1.invoke(tm,
mtkDoubleInfo.getSimId_2()));
Method mx = TelephonyManager.class.getDeclaredMethod(
"getPhoneTypeGemini", int.class);
mtkDoubleInfo.setPhoneType_1((Integer) mx.invoke(tm,
mtkDoubleInfo.getSimId_1()));
mtkDoubleInfo.setPhoneType_2((Integer) mx.invoke(tm,
mtkDoubleInfo.getSimId_2()));
if (TextUtils.isEmpty(mtkDoubleInfo.getImsi_1())
&& (!TextUtils.isEmpty(mtkDoubleInfo.getImsi_2()))) {
mtkDoubleInfo.setDefaultImsi(mtkDoubleInfo.getImsi_2());
}
if (TextUtils.isEmpty(mtkDoubleInfo.getImsi_2())
&& (!TextUtils.isEmpty(mtkDoubleInfo.getImsi_1()))) {
mtkDoubleInfo.setDefaultImsi(mtkDoubleInfo.getImsi_1());
}
} catch (Exception e) {
mtkDoubleInfo.setMtkDoubleSim(false);
return mtkDoubleInfo;
}
mtkDoubleInfo.setMtkDoubleSim(true);
return mtkDoubleInfo;
}
直接判斷異常。出現異常證明就不是MTK平臺了。
1.2判斷手機是否高通平臺
public static GaotongDoubleInfo initQualcommDoubleSim(Context mContext) {
GaotongDoubleInfo gaotongDoubleInfo = new GaotongDoubleInfo();
gaotongDoubleInfo.setSimId_1(0);
gaotongDoubleInfo.setSimId_2(1);
try {
Class<!--?--> cx = Class
.forName("android.telephony.MSimTelephonyManager");
Object obj = mContext.getSystemService("phone_msim");
Method md = cx.getMethod("getDeviceId", int.class);
Method ms = cx.getMethod("getSubscriberId", int.class);
gaotongDoubleInfo.setImei_1((String) md.invoke(obj,
gaotongDoubleInfo.getSimId_1()));
gaotongDoubleInfo.setImei_2((String) md.invoke(obj,
gaotongDoubleInfo.getSimId_2()));
gaotongDoubleInfo.setImsi_1((String) ms.invoke(obj,
gaotongDoubleInfo.getSimId_1()));
gaotongDoubleInfo.setImsi_2((String) ms.invoke(obj,
gaotongDoubleInfo.getSimId_2()));
} catch (Exception e) {
e.printStackTrace();
gaotongDoubleInfo.setGaotongDoubleSim(false);
return gaotongDoubleInfo;
}
return gaotongDoubleInfo;
}
一樣出現異常就不是高通雙卡平臺了
然後我們在整理下邏輯
/**
* @param c
* @return 返回平臺數據
*/
public Object isDoubleSim(Context c) {
GaotongDoubleInfo gaotongDoubleInfo = MultiSimUtility
.initQualcommDoubleSim(c);
MtkDoubleInfo mtkDoubleInfo = MultiSimUtility.initMtkDoubleSim(c);
boolean isGaoTongCpu = gaotongDoubleInfo.isGaotongDoubleSim();
boolean isMtkCpu = mtkDoubleInfo.isMtkDoubleSim();
if (isGaoTongCpu) {
// 高通芯片雙卡
return gaotongDoubleInfo;
} else if (isMtkCpu) {
// MTK芯片雙卡
return mtkDoubleInfo;
} else {
//普通單卡手機
return null;
}
}