get SIM Slot Id Ready - Android Phone

Android examples for Phone:Sim

Description

get SIM Slot Id Ready

Demo Code


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

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

public class Main {

    public static int getSlotIdReady(Context context) {

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

        if (isSim1Ready && isSim2Ready) { 
            return 2;

        } else if (isSim1Ready && !isSim2Ready) { 
            return 0;

        } else if (!isSim1Ready && isSim2Ready) {
            return 1;

        } else if (!isSim1Ready && !isSim2Ready) { 
            return -1;
        }//from ww  w.  j  a  v a  2 s .c o  m

        return 0;
    }

    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