Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果在其中一个双Sim卡插槽中更改了Sim卡,我如何检索该卡的信息?_Android_Android Broadcastreceiver - Fatal编程技术网

Android 如果在其中一个双Sim卡插槽中更改了Sim卡,我如何检索该卡的信息?

Android 如果在其中一个双Sim卡插槽中更改了Sim卡,我如何检索该卡的信息?,android,android-broadcastreceiver,Android,Android Broadcastreceiver,我知道我们可以使用TelephonyManager类检索单个Sim卡的信息。但是如何检索多张sim卡的信息?比如说,我的手机丢了,有人换了其中一个插槽中的sim卡。我如何接收信息,包括IMEI号码、电话号码等?我只在更换Sim卡时才需要这些信息 在TelephonyManager类中,有直接的方法来检索这些信息(如IMEI号码、电话号码、电话网络类型等),但这些信息仅可用于一张Sim卡。如何接收有关多张sim卡的所有信息。 P.S-SubscriptionManager类没有像Telephony

我知道我们可以使用TelephonyManager类检索单个Sim卡的信息。但是如何检索多张sim卡的信息?比如说,我的手机丢了,有人换了其中一个插槽中的sim卡。我如何接收信息,包括IMEI号码、电话号码等?我只在更换Sim卡时才需要这些信息

在TelephonyManager类中,有直接的方法来检索这些信息(如IMEI号码、电话号码、电话网络类型等),但这些信息仅可用于一张Sim卡。如何接收有关多张sim卡的所有信息。 P.S-SubscriptionManager类没有像TelephonyManager类那样的直接方法

    import android.content.Context;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.widget.TextView;

import java.util.List;

import static android.R.id.content;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        int subID,simposition,content;
        String carrier,number;
        TextView textView2=(TextView)findViewById(R.id.text);
        SubscriptionManager subManager=(SubscriptionManager)getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);

        List<SubscriptionInfo> subInfoList=subManager.getActiveSubscriptionInfoList();

        for(int i=0;i<subInfoList.size();i++)
        {
           subID = subInfoList.get(i).getSubscriptionId();
            simposition = subInfoList.get(i).getSimSlotIndex();
             content=subInfoList.get(i).describeContents();
          carrier= (String) subInfoList.get(i).getCarrierName();
            number= subInfoList.get(i).getNumber();
        }

       /* String info2="Sim Details:\n";
        info2+="\n SubId:"+subID;
        info2+="\n Sim position:"+simposition;
        info2+="\n contents:"+content;
        info2+="\n carrier:"+carrier;
        info2+="\n number:"+number;
        textView2.setText(info2);*/
    }
}
导入android.content.Context;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.telephony.SubscriptionInfo;
导入android.telephony.SubscriptionManager;
导入android.widget.TextView;
导入java.util.List;
导入静态android.R.id.content;
公共类MainActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int subID,simposition,content;
字符串载体,数字;
TextView textView2=(TextView)findViewById(R.id.text);
SubscriptionManager subManager=(SubscriptionManager)getSystemService(Context.TELEPHONY\u SUBSCRIPTION\u SERVICE);
List subInfoList=subManager.getActiveSubscriptionInfoList();

对于(inti=0;i,您可以使用此工作示例代码来解决您的问题

参考链接:

类中的示例代码:

/**
*由piotrz于2015年7月23日创建。
*版权所有2015 Piotr Zerynger ITger
*/
导入android.content.Context;
导入android.telephony.CellInfo;
导入android.telephony.TelephonyManager;
导入android.telephony.gsm.GsmCellLocation;
导入java.lang.reflect.Method;
导入java.util.ArrayList;
导入java.util.List;
导入静态pl.itger.dualsimcard.MainActivity.getMActContext;
公共最终类TelInfo{
私有静态TelInfo telInf;
受保护的科学;
ArrayList scitemsArr;
//私有字符串imsiSIM1;
私有字符串imsiSIM2;
私有布尔值准备就绪;
私有布尔值准备就绪;
私有字符串sim1_状态;
私有字符串sim2_状态;
//集成电路卡标识符(ICCID)
私有字符串sim1_ICCID;
私有字符串sim2_ICCID;
//国际移动用户标识(IMSI)
私有字符串sim1_IMSI;
私有字符串sim2_IMSI;
//服务提供商名称(SPN)
私有字符串sim1_SPN;
私有字符串sim2_SPN;
//移动国家代码(MCC)
专用字符串sim1_-MCC;
专用字符串sim2_-MCC;
//移动网络代码(MNC)
私有字符串sim1_跨国公司;
私有字符串sim2_跨国公司;
//私人邻居CellInfo nci;
//私有列表sim1\u NC;
//私有列表sim2_NC;
私有字符串sim1_MCC_MNC;
私有字符串sim2_MCC_MNC;
//移动用户标识号(MSIN)
//移动台国际用户目录号(MSISDN)
私有字符串sim1_MSISDN;
私有字符串sim2_MSISDN;
//缩写拨号号码(ADN)
//最后拨打的号码(LDN)
//短消息服务(SMS)
//语言偏好(LP)
//持卡人验证(CHV1和CHV2)
//加密密钥(Kc)
//加密密钥序列号
//紧急呼叫代码
//固定拨号号码(FDN)
//getDeviceId()返回唯一的设备ID,例如,GSM的IMEI和CDMA电话的MEID或ESN。
私有字符串sim1_IMEI;
私有字符串sim2_IMEI;
//本地区身份(LAI)
私人字符串sim1_-LAI;
私人字符串sim2_-LAI;
//位置区号(LAC)。
私有字符串sim1_LAC;
私有字符串sim2_LAC;
//自己的拨号号码
//临时移动用户标识(TMSI)
//路由区域标识符(RIA)网络代码
//服务拨号号码(SDN)
//细胞
私有字符串sim1_CellID;
私有字符串sim2_CellID;
私人名单所有;
私人电讯资讯({
}
/*@
@需要上下文!=null;
@确保TelInfo!=null;
@*/
公共静态TelInfo getInstance(){//上下文){
//如果(TelInfo.telInf!=null){
TelInfo.telInf=null;
TelInfo.telInf=新的TelInfo();
//}
Context=getMActContext();
//打印TelephonyManagerMethodNamesforthisDevice(上下文);
//System.out.println(“1”+上下文);
TelephonyManager telMngr=((TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE));
//系统输出打印项次(“2”+telMngr);
//telInf.nci=new neightringcellinfo(neightringcellinfo.UNKNOWN\u RSSI,new GsmCellLocation().toString(),TelephonyManager.NETWORK\u TYPE\u HSPA);
//System.out.println(telInf.nci.toString());
telInf.sim1_ICCID=telMngr.getSimSerialNumber();
telInf.sim1_IMSI=telMngr.getSubscriberId();
telInf.sim1_SPN=telMngr.getSimOperatorName();
telInf.sim1_MCC=telMngr.getNetworkCountryIso();
telInf.sim1_MNC=telMngr.getNetworkOperatorName();
telInf.sim1_MCC_MNC=telMngr.getSimOperator();
telInf.sim1_MSISDN=telMngr.getLine1Number();
telInf.sim1_IMEI=telMngr.getDeviceId();
//telInf.sim1\u NC=telMng
    /**
     * Created by piotrz on 7/23/15.
     * Copyright 2015 Piotr Zerynger ITger
     */

    import android.content.Context;
    import android.telephony.CellInfo;
    import android.telephony.TelephonyManager;
    import android.telephony.gsm.GsmCellLocation;

    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;

    import static pl.itger.dualsimcard.MainActivity.getMActContext;

    public final class TelInfo {

        private static TelInfo telInf;
        protected Sci[] scitems;
        ArrayList<Sci> scitemsArr;
        //private String imsiSIM1;
        private String imsiSIM2;
        private boolean isSIM1Ready;
        private boolean isSIM2Ready;
        private String sim1_STATE;
        private String sim2_STATE;
        // Integrated circuit card identifier (ICCID)
        private String sim1_ICCID;
        private String sim2_ICCID;
        // International mobile subscriber identity (IMSI)
        private String sim1_IMSI;
        private String sim2_IMSI;
        // Service provider name (SPN)
        private String sim1_SPN;
        private String sim2_SPN;
        // Mobile country code (MCC)
        private String sim1_MCC;
        private String sim2_MCC;
        // Mobile network code (MNC)
        private String sim1_MNC;
        private String sim2_MNC;

        //private NeighboringCellInfo nci;

        //private  List<NeighboringCellInfo> sim1_NC;
        //private  List<NeighboringCellInfo> sim2_NC;
        private String sim1_MCC_MNC;
        private String sim2_MCC_MNC;
        // Mobile subscriber identification number (MSIN)
        // Mobile station international subscriber directory number (MSISDN)
        private String sim1_MSISDN;
        private String sim2_MSISDN;


        // Abbreviated dialing numbers (ADN)
        // Last dialed numbers (LDN)
        // Short message service (SMS)
        // Language preference (LP)
        // Card holder verification (CHV1 and CHV2)
        // Ciphering key (Kc)
        // Ciphering key sequence number
        // Emergency call code
        // Fixed dialing numbers (FDN)
        //getDeviceId() Returns the unique device ID, for example, the IMEI for GSM and the MEID or ESN for CDMA phones.
        private String sim1_IMEI;
        private String sim2_IMEI;
        // Local area identity (LAI)
        private String sim1_LAI;
        private String sim2_LAI;
        // Location Area Code (LAC).
        private String sim1_LAC;
        private String sim2_LAC;


        // Own dialing number
        // Temporary mobile subscriber identity (TMSI)
        // Routing area identifier (RIA) network code
        // Service dialing numbers (SDNs)
        // CellID
        private String sim1_CellID;
        private String sim2_CellID;
        private List<CellInfo> all;


        private TelInfo() {
        }

        /*@
       @requires context != null;
       @ensures TelInfo != null;
          @*/
        public static TelInfo getInstance() {//Context context) {
            //if (TelInfo.telInf != null) {
            TelInfo.telInf = null;
            TelInfo.telInf = new TelInfo();
            //}
            Context context = getMActContext();
            //printTelephonyManagerMethodNamesForThisDevice(context);
            //System.out.println("1 " + context);

            TelephonyManager telMngr = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));
            //System.out.println("2 " + telMngr);

            //telInf.nci = new NeighboringCellInfo(NeighboringCellInfo.UNKNOWN_RSSI, new GsmCellLocation().toString(), TelephonyManager.NETWORK_TYPE_HSPA);
            //System.out.println(telInf.nci.toString());


            telInf.sim1_ICCID = telMngr.getSimSerialNumber();
            telInf.sim1_IMSI = telMngr.getSubscriberId();
            telInf.sim1_SPN = telMngr.getSimOperatorName();
            telInf.sim1_MCC = telMngr.getNetworkCountryIso();
            telInf.sim1_MNC = telMngr.getNetworkOperatorName();
            telInf.sim1_MCC_MNC = telMngr.getSimOperator();
            telInf.sim1_MSISDN = telMngr.getLine1Number();
            telInf.sim1_IMEI = telMngr.getDeviceId();
            //telInf.sim1_NC = telMngr.getNeighboringCellInfo();

            if (telMngr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                final GsmCellLocation location = (GsmCellLocation) telMngr.getCellLocation();
                if (location != null) {
                    telInf.sim1_LAC = Integer.toString(location.getLac());
                    telInf.sim1_CellID = Integer.toString(location.getCid());
                }
            }

            telInf.imsiSIM2 = null;

            telInf.isSIM1Ready = telMngr.getSimState() == TelephonyManager.SIM_STATE_READY;
            telInf.isSIM2Ready = false;

            telInf.sim1_STATE = simState(telMngr.getSimState());
            try {
                //telInf.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
                telInf.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
                //System.out.println("!!");
            } catch (ITgerMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }

            if (telInf.isSIM2Ready) {
                try {
                    // telInf.sim1_IMSI = getDeviceIdBySlot(context, "getDeviceId", 0);
                    telInf.imsiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
                    telInf.sim2_ICCID = getDeviceIdBySlot(context, "getSimSerialNumber", 1);
                    telInf.sim2_IMSI = getDeviceIdBySlot(context, "getSubscriberId", 1);
                    telInf.sim2_SPN = getDeviceIdBySlot(context, "getSimOperatorName", 1);
                    telInf.sim2_MCC = getDeviceIdBySlot(context, "getNetworkCountryIso", 1);
                    telInf.sim2_MNC = getDeviceIdBySlot(context, "getNetworkOperatorName", 1);
                    telInf.sim2_MCC_MNC = getDeviceIdBySlot(context, "getSimOperator", 1);
                    telInf.sim2_MSISDN = getDeviceIdBySlot(context, "getLine1Number", 1);
                    telInf.sim2_IMEI = getDeviceIdBySlot(context, "getDeviceId", 1);

                    if (telMngr.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
                        final GsmCellLocation location = getCellLocBySlot(context, "getCellLocation", 1);// telMngr.getCellLocation();
                        if (location != null) {
                            telInf.sim2_LAC = Integer.toString(location.getLac());
                            telInf.sim2_CellID = Integer.toString(location.getCid());
                        }
                    }

                    System.out.println("!");
                } catch (ITgerMethodNotFoundException e1) {
                    //Call here for next manufacturer's predicted method name if you wish
                    e1.printStackTrace();
                }
            }

            telInf.scitemsArr = new ArrayList<Sci>();

            telInf.scitemsArr.add(new Sci("SIM 1 state", telInf.sim1_STATE));
            telInf.scitemsArr.add(new Sci("Integrated circuit card identifier (ICCID)", telInf.sim1_ICCID));
            telInf.scitemsArr.add(new Sci("Unique device ID (IMEI or MEID/ESN for CDMA)", telInf.sim1_IMEI));
            telInf.scitemsArr.add(new Sci("International mobile subscriber identity (IMSI)", telInf.sim1_IMSI));
            telInf.scitemsArr.add(new Sci("Service provider name (SPN)", telInf.sim1_SPN));
            telInf.scitemsArr.add(new Sci("Mobile country code (MCC)", telInf.sim1_MCC));
            telInf.scitemsArr.add(new Sci("Mobile operator name", telInf.sim1_MNC));
            telInf.scitemsArr.add(new Sci("Mobile country code + mobile network code (MCC+MNC)", telInf.sim1_MCC_MNC));
            telInf.scitemsArr.add(new Sci("Mobile station international subscriber directory number (MSISDN)", telInf.sim1_MSISDN));
            telInf.scitemsArr.add(new Sci("Location Area Code (LAC)", telInf.sim1_LAC));
            telInf.scitemsArr.add(new Sci("Cell Tower ID (CID)", telInf.sim1_CellID));

            if (telInf.isSIM2Ready) {
                telInf.scitemsArr.add(new Sci(" ", " "));
                telInf.scitemsArr.add(new Sci("SIM 2 state", telInf.sim2_STATE));
                telInf.scitemsArr.add(new Sci("Integrated circuit card identifier (ICCID)", telInf.sim2_ICCID));
                telInf.scitemsArr.add(new Sci("Unique device ID (IMEI or MEID/ESN for CDMA)", telInf.sim2_IMEI));
                telInf.scitemsArr.add(new Sci("International mobile subscriber identity (IMSI)", telInf.sim2_IMSI));
                telInf.scitemsArr.add(new Sci("Service provider name (SPN)", telInf.sim2_SPN));
                telInf.scitemsArr.add(new Sci("Mobile country code (MCC)", telInf.sim2_MCC));
                telInf.scitemsArr.add(new Sci("Mobile operator name", telInf.sim2_MNC));
                telInf.scitemsArr.add(new Sci("Mobile country code + mobile network code (MCC+MNC)", telInf.sim2_MCC_MNC));
                telInf.scitemsArr.add(new Sci("Mobile station international subscriber directory number (MSISDN)", telInf.sim2_MSISDN));
                telInf.scitemsArr.add(new Sci("Location Area Code (LAC)", telInf.sim2_LAC));
                telInf.scitemsArr.add(new Sci("Cell Tower ID (CID)", telInf.sim2_CellID));


                //telInf.scitemsArr.add(new Sci("NC (Neighboring Cell ", telInf.sim1_NC.toString() ));

            }

            return telInf;
        }

        private static String simState(int simState) {
            switch (simState) {
                case 0:
                    return "UNKNOWN";
                case 1:
                    return "ABSENT";
                case 2:
                    return "REQUIRED";
                case 3:
                    return "PUK_REQUIRED";
                case 4:
                    return "NETWORK_LOCKED";
                case 5:
                    return "READY";
                case 6:
                    return "NOT_READY";
                case 7:
                    return "PERM_DISABLED";
                case 8:
                    return "CARD_IO_ERROR";
            }
            return "??? " + simState;
        }

        private static String getDeviceIdBySlot(Context context, String predictedMethodName, int slotID) throws ITgerMethodNotFoundException {

            String imsi = null;
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
                Class<?>[] parameter = new Class[1];
                parameter[0] = int.class;
                Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

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

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

                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ITgerMethodNotFoundException(predictedMethodName);
            }

            return imsi;
        }

        private static GsmCellLocation getCellLocBySlot(Context context, String predictedMethodName, int slotID) throws ITgerMethodNotFoundException {

            GsmCellLocation cloc = null;
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
                Class<?>[] parameter = new Class[1];
                parameter[0] = int.class;
                Method getSimID = telephonyClass.getMethod(predictedMethodName, parameter);

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

                if (ob_phone != null) {
                    cloc = (GsmCellLocation) ob_phone;

                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ITgerMethodNotFoundException(predictedMethodName);
            }

            return cloc;
        }

        private static boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws ITgerMethodNotFoundException {
            boolean isReady = false;
            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            try {
                Class<?> telephonyClass = Class.forName(telephony.getClass().getName());
                Class<?>[] parameter = new Class[1];
                parameter[0] = int.class;
                Method getSimState = telephonyClass.getMethod(predictedMethodName, parameter);
                Object[] obParameter = new Object[1];
                obParameter[0] = slotID;
                Object ob_phone = getSimState.invoke(telephony, obParameter);

                if (ob_phone != null) {
                    int simState = Integer.parseInt(ob_phone.toString());
                    telInf.sim2_STATE = simState(simState);
                    if ((simState != TelephonyManager.SIM_STATE_ABSENT) && (simState != TelephonyManager.SIM_STATE_UNKNOWN)) {
                        isReady = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new ITgerMethodNotFoundException(predictedMethodName);
            }

            return isReady;
        }

        public static void printTelephonyManagerMethodNamesForThisDevice(Context context) {

            TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> telephonyClass;
            try {
                System.out.println("-------------------------------");
                telephonyClass = Class.forName(telephony.getClass().getName());
                Method[] methods = telephonyClass.getMethods();

                for (Method method : methods) {
                    // if (methods[idx].getName().startsWith("get")) {
                    System.out.println("\n" + method.getName() + " declared by " + method.getDeclaringClass());//+ "  superclass " + methods[idx].getDeclaringClass().getSuperclass().getName());
                    //}
                }
                System.out.println("-------------------------------");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        public boolean isSIM1Ready() {
            return isSIM1Ready;
        }

        public boolean isSIM2Ready() {
            return isSIM2Ready;
        }

        private boolean isDualSIM() {
            return imsiSIM2 != null;
        }

        @Override
        public String toString() {
            return "XXXXXXXXXXXXXXXX TelInfo{" +
                    "imsiSIM1='" + sim1_IMSI + '\'' +
                    ", imsiSIM2='" + imsiSIM2 + '\'' +
                    ", isSIM1Ready=" + isSIM1Ready +
                    ", isSIM2Ready=" + isSIM2Ready +
                    '}';
        }

        private static class ITgerMethodNotFoundException extends Exception {
            private static final long serialVersionUID = -996812356902545308L;

            public ITgerMethodNotFoundException(String info) {
                super(info);
            }

        }
    }