get SIM Imsi By Slot - Android Phone

Android examples for Phone:Sim

Description

get SIM Imsi By Slot

Demo Code


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

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

public class Main {

    public static String getImsiBySlot(Context context, int slotID)
            throws Exception {
        String imsi = "";
        if (context == null) {
            return "";
        }//from   ww w.j av a 2 s .com

        if (slotID < 0 || slotID > 1) {
            return "";
        }

        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 getSubscriberIdGemini = mLoadClass.getMethod(
                "getSubscriberIdGemini", parameter);

        Object ob_imsi = null;
        Object[] obParameter = new Object[1];
        obParameter[0] = slotID;
        ob_imsi = getSubscriberIdGemini.invoke(telephonyManager,
                obParameter);

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

        }

        return imsi;
    }
}

Related Tutorials