Android getPhoneType获取当前phone类型不准

时间:2021-11-20 20:31:35

Android开发中,可能需要区分当前的phone是gsm或cdma类型,有时候cdma phone注册到4G网络时,调用Phone@getPhoneType获取到的类型为PHONE_TYPE_GSM,其实除了这个方法获取sim卡类型,还可以根据iccid会mccmnc来区分当前的sim卡类型,甚至可以区分得更详细(移动、联通、电信),具体方式如下:

//Only one sim card
private static final int PHONE_TYPE_CMCC = 0;
private static final int PHONE_TYPE_UNICOM = 1;
private static final int PHONE_TYPE_CDMA = 2;

private int getPhoneType() {
    int phoneType = -1;
    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    String mccMnc = tm.getSimOperator();
    if ("46000".equals(mccMnc) || "46002".equals(mccMnc)
            || "46007".equals(mccMnc) || "46008".equals(mccMnc)
            || "45412".equals(mccMnc)) {
        phoneType = PHONE_TYPE_CMCC;
    } else if ("46001".equals(mccMnc) || "46006".equals(mccMnc)
            || "46009".equals(mccMnc)) {
        phoneType = PHONE_TYPE_UNICOM;
    } else if ("46003".equals(mccMnc) || "46005".equals(mccMnc)
            || "46011".equals(mccMnc) || "45502".equals(mccMnc)
            || "45507".equals(mccMnc)) {
        phoneType = PHONE_TYPE_CDMA;
    }
    return phoneType;
}

这样可以直接区分当前sim卡所属的运营商,然后根据运营商区分PHONE_TYPE_GSM(移动、联通)或PHONE_TYPE_CDMA(电信)类型