Android 获取运营商信息(完整版)-解决高通,MTK等双卡问题

由于国内的运营商问题,双卡手机获取IMSI号问题要根据厂商API 来实现。

下面我们就来做一套完整的分析运营商获取IMSI号逻辑。

1,首先我们要判断手机的平台。

    1.1,判断手机是否MTK平台

	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;
		}
	}

这个时候我们基本上获取到了手机的平台和每张卡的信息。单卡手机获取信息就不贴代码了。



你可能感兴趣的:(Android,进阶开发)