简体   繁体   English

如何在Android中为双卡获取simOperator

[英]how to get simOperator for a dual sim in android

我正在创建一个需要为双SIM卡设备检测2 SimOperateur的android应用程序,但我没有找到解决方案

I find this code and I added getSimOperatorBySlot by it didn't work exactly 我找到了这段代码,并添加了getSimOperatorBySlot,因为它无法正常工作

import java.lang.reflect.Method; 导入java.lang.reflect.Method;

import android.content.Context; 导入android.content.Context; import android.telephony.TelephonyManager; 导入android.telephony.TelephonyManager;

public final class TelephonyInfo { 公共最终课程TelephonyInfo {

private static TelephonyInfo mTelephonyInfo;
private String imeiSIM1;
private String imeiSIM2;


private String mOperatorCode;




private boolean isSIM1Ready;
private boolean isSIM2Ready;


public String getImeiSIM1() {
    return imeiSIM1;
}

/*
 * public static void setImeiSIM1(String imeiSIM1) { TelephonyInfo.imeiSIM1
 * = imeiSIM1; }
 */

public String getImeiSIM2() {
    return imeiSIM2;
}

/*
 * public static void setImeiSIM2(String imeiSIM2) { TelephonyInfo.imeiSIM2
 * = imeiSIM2; }
 */

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 boolean isDualSIM() {
    return imeiSIM2 != null;
}



private TelephonyInfo() {
}

public static TelephonyInfo getInstance(Context context) {

    if (mTelephonyInfo == null) {

        mTelephonyInfo = new TelephonyInfo();

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



        mTelephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();
        mTelephonyInfo.imeiSIM2 = null;


        try {
            mTelephonyInfo.imeiSIM1 = getDeviceIdBySlot(context,
                    "getDeviceIdGemini", 0);
            mTelephonyInfo.imeiSIM2 = getDeviceIdBySlot(context,
                    "getDeviceIdGemini", 1);

        } catch (GeminiMethodNotFoundException e) {
            e.printStackTrace();

            try {
                mTelephonyInfo.imeiSIM1 = getDeviceIdBySlot(context,
                        "getDeviceId", 0);
                mTelephonyInfo.imeiSIM2 = getDeviceIdBySlot(context,
                        "getDeviceId", 1);

            } catch (GeminiMethodNotFoundException e1) {
                // Call here for next manufacturer's predicted method name
                // if you wish
                e1.printStackTrace();
                try {
                    mTelephonyInfo.imeiSIM1= getSimOperatorBySlot(context,
                            "getSimOperator",0);
                    mTelephonyInfo.imeiSIM2= getSimOperatorBySlot(context,
                            "getSimOperator",1);

                }catch (GeminiMethodNotFoundException e2) {
            }
        }
    }
        mTelephonyInfo.mOperatorCode = telephonyManager.getSimOperator();



        if (telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY) {
            mTelephonyInfo.isSIM1Ready = true;
        } else {
            mTelephonyInfo.isSIM1Ready = false;
        }
        mTelephonyInfo.isSIM2Ready = false;


        try {
            mTelephonyInfo.isSIM1Ready = getSIMStateBySlot(context,
                    "getSimStateGemini", 0);
            mTelephonyInfo.isSIM2Ready = getSIMStateBySlot(context,
                    "getSimStateGemini", 1);

        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                mTelephonyInfo.isSIM1Ready = getSIMStateBySlot(context,
                        "getSimState", 0);
                mTelephonyInfo.isSIM2Ready = getSIMStateBySlot(context,
                        "getSimState", 1);


            } catch (GeminiMethodNotFoundException e1) {
                // Call here for next manufacturer's predicted method name
                // if you wish
                e1.printStackTrace();


            }
        }
    }

    return mTelephonyInfo;
}

public String getmOperatorCode() {
    return mOperatorCode;
}

/*public void setmOperatorCode(String mOperatorCode) {
    mOperatorCode = mOperatorCode;
}*/

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

    String imei = 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){
            imei = ob_phone.toString();

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

    return imei;
}

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 getSimOperatorBySlot(Context context,
        String predictedMethodName, int slotID)
        throws GeminiMethodNotFoundException {

    String imei = 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) {
            imei = ob_phone.toString();

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

    return imei;
}

private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM