get Imsi By Sim ID - Android Phone

Android examples for Phone:Sim

Description

get Imsi By Sim ID

Demo Code


import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;
import android.telephony.TelephonyManager;
import java.lang.reflect.Method;

public class Main{
    /*from  w  w w  .  ja va2 s.  co m*/
    public static String getImsiBySimID(Context context, long sim_id) {
        String imsi = "";

        if (context == null || sim_id < 1) {
            return "";
        }
        try {
            SimCardInfo info = getSimInfoBySlot(context, 0); //?1
            if (info != null) {
                if (info.mSimId == sim_id) {
                    imsi = getImsiBySlot(context, 0);
                    return imsi;
                }
            }

            info = getSimInfoBySlot(context, 1); //?2
            if (info != null) {
                if (info.mSimId == sim_id) {
                    imsi = getImsiBySlot(context, 1);
                    return imsi;
                }
            }
        } catch (Exception e) {

        }
        return imsi;
    }
    
    public static SimCardInfo getSimInfoBySlot(Context context, int slotID) {
        if (context == null || slotID < 0) {
            return null;
        }

        try {
            Cursor cursor = context.getContentResolver().query(
                    SimInfo.CONTENT_URI, null, SimInfo.SLOT + "=?",
                    new String[] { String.valueOf(slotID) }, null);
            try {
                if (cursor != null) {
                    if (cursor.moveToFirst()) {
                        return fromCursor(cursor);
                    }
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        } catch (Exception e) {

        }
        return null;
    }
    
    public static String getImsiBySlot(Context context, int slotID)
            throws Exception {
        String imsi = "";
        if (context == null) {
            return "";
        }

        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;
    }
    
    public static SimCardInfo fromCursor(Cursor cursor) {
        if (cursor == null) {
            return null;
        }

        SimCardInfo info = new SimCardInfo();
        try {
            info.mSimId = cursor.getLong(cursor
                    .getColumnIndexOrThrow(DoubleSimPhoneUtil.SimInfo._ID));
            info.mICCId = cursor.getString(cursor
                    .getColumnIndexOrThrow(SimInfo.ICC_ID));
            info.mDisplayName = cursor.getString(cursor
                    .getColumnIndexOrThrow(SimInfo.DISPLAY_NAME));
            info.mNumber = cursor.getString(cursor
                    .getColumnIndexOrThrow(SimInfo.NUMBER));
            info.mDispalyNumberFormat = cursor.getInt(cursor
                    .getColumnIndexOrThrow(SimInfo.DISPLAY_NUMBER_FORMAT));
            info.mColor = cursor.getInt(cursor
                    .getColumnIndexOrThrow(SimInfo.COLOR));
            info.mDataRoaming = cursor.getInt(cursor
                    .getColumnIndexOrThrow(SimInfo.DATA_ROAMING));
            info.mSlot = cursor.getInt(cursor
                    .getColumnIndexOrThrow(SimInfo.SLOT));
        } catch (Exception e) {

        }
        return info;
    }
}

Related Tutorials