get Default Sim - Android Phone

Android examples for Phone:Sim

Description

get Default Sim

Demo Code


//package com.java2s;

import java.lang.reflect.Method;

public class Main {

    public static int getDefaultSim() {
        int slotID = 0;
        try {/*from   ww  w  . j a v  a 2  s  .c o m*/
            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;
    }
}

Related Tutorials