当手机是双卡时,请在Android上获取这两家运营商的名称

时间:2016-11-24 12:19:54

标签: java android mobile

我需要在Android手机中使用DUAL SIM系统获取SIM卡运营商的名称,SIM卡套装的数据为主,我已经可以获得。但来自SIM卡的数据并不是主要的,我遇到了麻烦。我需要将很多内容应用到我正在处理的项目中,因为我需要列出手机上的两个运营商。

目前我能够进化的是这段代码

public final class TelephonyInfo {

private static TelephonyInfo telephonyInfo;
private String imsiSIM1;
private String imsiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;
private String networkOperatorOne;
private String networkOperatorDual;

public String getImsiSIM1() {
    return imsiSIM1;
}

/*public static void setImsiSIM1(String imsiSIM1) {
    TelephonyInfo.imsiSIM1 = imsiSIM1;
}*/

public String getImsiSIM2() {
    return imsiSIM2;
}

/*public static void setImsiSIM2(String imsiSIM2) {
    TelephonyInfo.imsiSIM2 = imsiSIM2;
}*/

public boolean isSIM1Ready() {
    return isSIM1Ready;
}

/*public static void setSIM1Ready(boolean isSIM1Ready) {
    TelephonyInfo.isSIM1Ready = isSIM1Ready;
}*/

public boolean isSIM2Ready() {
    return isSIM2Ready;
}

/*public static void setSIM2Ready(boolean isSIM2Ready) {
    TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/

public String getNetworkOperatorOne() {
    return networkOperatorOne;
}

/*public void setNetworkOperatorOne(String networkOperatorOne) {
    this.networkOperatorOne = networkOperatorOne;
}*/

public String getNetworkOperatorDual() {
    return networkOperatorDual;
}

/*public void setNetworkOperatorDual(String networkOperatorDual) {
    this.networkOperatorDual = networkOperatorDual;
}*/

public boolean isDualSIM() {
    return imsiSIM2 != null;
}

private TelephonyInfo() {
}

public static TelephonyInfo getInstance(Context context){

    if(telephonyInfo == null) {

        telephonyInfo = new TelephonyInfo();

        TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

        telephonyInfo.imsiSIM1 = telephonyManager.getDeviceId();
        telephonyInfo.imsiSIM2 = null;

        telephonyInfo.networkOperatorOne =telephonyManager.getNetworkOperator();
        telephonyInfo.networkOperatorDual = null ;

        try {
            telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0);
            telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1);
            telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
            telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
        } catch (GeminiMethodNotFoundException e) {
            e.printStackTrace();

            try {
                telephonyInfo.imsiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0);
                telephonyInfo.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
                telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
                telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }

        telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
        telephonyInfo.isSIM2Ready = false;

        try {
            telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0);
            telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1);
            telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
            telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
                telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
                telephonyInfo.networkOperatorOne = getCarrierName(context,"getCarrierName",0);
                telephonyInfo.networkOperatorDual = getCarrierName(context,"getCarrierName",1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }
    }

    return telephonyInfo;
}

private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String imsi = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimID.invoke(telephony, obParameter);

        if(ob_phone != null){
            imsi = ob_phone.toString();

        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return imsi;
}

private static  boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    boolean isReady = false;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try{

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = getSimStateGemini.invoke(telephony, obParameter);

        if(ob_phone != null){
            int simState = Integer.parseInt(ob_phone.toString());
            if(simState == TelephonyManager.SIM_STATE_READY){
                isReady = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return isReady;
}

private static String getCarrierName(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    String carrier = null;

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    try {

        Class<?> telephonyClass = Class.forName(telephony.getClass().getName());

        Class<?>[] parameter = new Class[1];
        parameter[0] = int.class;
        Method sim = telephonyClass.getMethod(predictedMethodName, parameter);

        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        Object ob_phone = sim.invoke(telephony, obParameter);

        if (ob_phone != null) {
            carrier = ob_phone.toString();
            if (carrier.equals(TelephonyManager.SIM_STATE_READY)) {
                Log.d("Services", "servicessss" + carrier);
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return carrier;
}

private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

    public GeminiMethodNotFoundException(String info) {
        super(info);
    }
}
}

我需要在此代码段中获取此信息。

public void getWidgtesRefernence() {
    listView = (ListView) findViewById(R.id.listView1);
    listView.setOnItemClickListener(this);
    findViewById(R.id.layoutContinue).setOnClickListener(this);
    findViewById(R.id.layoutCancel).setOnClickListener(this);

    LocalPreferences pref = new LocalPreferences(this);
    try {

        TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);
        TelephonyManager manager = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
        String op = manager.getSimOperatorName();
        JSONArray operadorasArray = new JSONArray(pref.getPreferenceValueR(LocalPreferences.operadoras));

        for (int i = 0; i < operadorasArray.length(); i++) {
            try {
                JSONObject json = operadorasArray.getJSONObject(i);
                if (op.toLowerCase().contains(json.getString("codigo").toLowerCase())) {
                    operadoras = new JSONArray();
                    operadoras.put(json);
                    if (telephonyInfo.isDualSIM()) {
                      operadoras.put();
                    }
                    break;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        //operadoras = new JSONArray(pref.getPreferenceValueR(LocalPreferences.operadoras));
        listView.setAdapter(new ListAdapterSelectOperator(operadoras, this));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案