Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 獲取運營商信息(完整版)-解決高通,MTK等雙卡問題

Android 獲取運營商信息(完整版)-解決高通,MTK等雙卡問題

編輯:關於Android編程

由於國內的運營商問題,雙卡手機獲取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;
		}
	}

這個時候我們基本上獲取到了手機的平台和每張卡的信息。單卡手機獲取信息就不貼代碼了。



  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved