get SIM Slot By Operator - Android Phone

Android examples for Phone:Sim

Description

get SIM Slot By Operator

Demo Code


//package com.java2s;
import android.content.Context;

import android.telephony.TelephonyManager;
import java.lang.reflect.Method;

public class Main {

    public static int getSlotByOperator(Context context) {

        int slotID = 0;

        if (context == null) {
            return 0;
        }/*from   ww  w  . j  ava 2 s  .  co m*/
        try {
            //?
            int readyRlot = getSlotIdReady(context);

            if (readyRlot != 2) { //readyRlot = 2   ?1?2?
                return readyRlot;
            }

            boolean isSlot1LT = false; //?1 
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            Class<?> mLoadClass = Class
                    .forName("android.telephony.TelephonyManager");

            Class<?>[] parameter = new Class[1];
            parameter[0] = int.class;
            Method getSimOperatorGemini = mLoadClass.getMethod(
                    "getSimOperatorGemini", parameter);

            Object[] obParameter = new Object[1];
            obParameter[0] = 0;
            Object object = getSimOperatorGemini.invoke(telephonyManager,
                    obParameter);

            if (object != null) {
                String operator = object.toString();
                if (operator.equals("46000") || operator.equals("46002")) {//
                    obParameter[0] = 1;
                    object = getSimOperatorGemini.invoke(telephonyManager,
                            obParameter);
                    if (object != null) {
                        operator = object.toString();
                        if (operator.equals("46000")
                                || operator.equals("46002")) { //
                            slotID = getDefaultSim(); //?1 ?2  ? ?
                            return slotID;
                        }
                    }
                    return 0;
                } else if (operator.equals("46001")) { //
                    isSlot1LT = true;
                }
            }
            obParameter[0] = 1;
            object = getSimOperatorGemini.invoke(telephonyManager,
                    obParameter);
            if (object != null) {
                String operator = object.toString();
                if (operator.equals("46000") || operator.equals("46002")) { //
                    return 1;

                } else if (operator.equals("46001")) { //
                    if (isSlot1LT) {
                        slotID = getDefaultSim(); //?1 ?2  ? ?
                    } else {
                        return 1;
                    }

                } else { //
                    if (isSlot1LT) { //?1? ?1
                        return 0;
                    } else {
                        slotID = getDefaultSim(); //?1 ?2  ?
                    }
                }
            }
        } catch (Exception e) {

        }
        return slotID;
    }

    public static int getSlotIdReady(Context context) {

        boolean isSim1Ready = isSimReady(context, 0);
        boolean isSim2Ready = isSimReady(context, 1);

        if (isSim1Ready && isSim2Ready) { //?1?22
            return 2;

        } else if (isSim1Ready && !isSim2Ready) { //?1?22
            return 0;

        } else if (!isSim1Ready && isSim2Ready) { //?2?11
            return 1;

        } else if (!isSim1Ready && !isSim2Ready) { //?2,?1 
            return -1;
        }

        return 0;
    }

    public static int getDefaultSim() {
        int slotID = 0;
        try {
            Class<?> smsManagerClass = Class
                    .forName("android.telephony.SmsManager");

            Method method = smsManagerClass.getMethod("getDefault",
                    new Class[] {});

            Object smsManager = method.invoke(smsManagerClass,
                    new Object[] {});

            Method getDefaultSim = smsManagerClass.getDeclaredMethod(
                    "getDefaultSim", new Class[] {});

            getDefaultSim.setAccessible(true);

            Object object = getDefaultSim.invoke(smsManager,
                    new Object[] {});

            if (object != null) {
                slotID = Integer.parseInt(object.toString());
            }
        } catch (Exception e) {

        }
        return slotID;
    }

    public static boolean isSimReady(Context context, int slotID) {
        boolean isReady = false;
        if (context == null) {
            return false;
        }

        if (slotID < 0 || slotID > 1) {
            return false;
        }
        try {
            TelephonyManager telephonyManager = (TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);

            Class<?> mLoadClass = Class
                    .forName("android.telephony.TelephonyManager");

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

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

            if (ob_phone != null) {
                int simState = Integer.parseInt(ob_phone.toString());
                if (simState == TelephonyManager.SIM_STATE_READY) {
                    isReady = true;
                }

            }
        } catch (Exception e) {

        }

        return isReady;
    }
}

Related Tutorials