在双卡Android手机中获取两个SIM卡号

22

我想获取双卡设备中的两个SIM卡号。


http://stackoverflow.com/questions/5163803/how-to-get-the-telephone-number-associated-with-the-sim-in-a-gsm-phone - Nirav Ranpara
但是在双卡手机中,我可以获取两张SIM卡的号码吗? - unflagged.destination
https://dev59.com/eWXWa4cB1Zd3GeqPMWWs - Nirav Ranpara
你有任何解决方案吗,unflagged.destination? - Wayne
我没有得到任何解决方案。我正在使用他们的默认数字来运行我的应用程序。 - unflagged.destination
检查手机是否支持双卡:Check whether the phone is dual SIM - Adnan Abdollah Zaki
3个回答

9
与 API > 21 一起使用
需要权限android.Manifest.permission.READ_PHONE_STATE。
public class SplahActivity extends AppCompatActivity {

    private Context context;
    private SubscriptionManager mSubscriptionManager;

    public static boolean isMultiSimEnabled = false;
    public static String defaultSimName;

    public static List<SubscriptionInfo> subInfoList;
    public static ArrayList<String> Numbers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_splah);

        Numbers = new ArrayList<String>();
        mSubscriptionManager = SubscriptionManager.from(context);
        GetCarriorsInformation();

    }

    private void GetCarriorsInformation() {
        subInfoList = mSubscriptionManager.getActiveSubscriptionInfoList();
        if (subInfoList.size() > 1) {
            isMultiSimEnabled = true;
        }
        for (SubscriptionInfo subscriptionInfo : subInfoList) {
            Numbers.add(subscriptionInfo.getNumber());
        }
    }
}

Android官方22版本的订阅管理器API


1
我尝试了这个,但是subscriptionInfo.getNumber()总是返回null。 - mindw0rk
1
@mindw0rk 它将从您的 SIM 设置中返回存储的数字。[http://i.imgur.com/GsH04DQ.png] - Sohail Zahid

5
请将这个类命名为TelephonyInfo.java:
import java.lang.reflect.Method;

import android.content.Context;
import android.telephony.TelephonyManager;

public final class TelephonyInfo {

private static TelephonyInfo telephonyInfo;
private String imeiSIM1;
private String imeiSIM2;
private boolean isSIM1Ready;
private boolean isSIM2Ready;

public String getImeiSIM1() {
    return imeiSIM1;
}

/*public static void setImeiSIM1(String imeiSIM1) {
    TelephonyInfo.imeiSIM1 = imeiSIM1;
}*/

public String getImeiSIM2() {
    return imeiSIM2;
}

/*public static void setImeiSIM2(String imeiSIM2) {
    TelephonyInfo.imeiSIM2 = imeiSIM2;
}*/

public boolean isSIM1Ready() {
    return isSIM1Ready;
}

/*public static void setSIM1Ready(boolean isSIM1Ready) {
    TelephonyInfo.isSIM1Ready = isSIM1Ready;
}*/

public boolean isSIM2Ready() {
    return isSIM2Ready;
}

/*public static void setSIM2Ready(boolean isSIM2Ready) {
    TelephonyInfo.isSIM2Ready = isSIM2Ready;
}*/

public boolean isDualSIM() {
    return imeiSIM2 != null;
}

private TelephonyInfo() {
}

public static TelephonyInfo getInstance(Context context){

    if(telephonyInfo == null) {

        telephonyInfo = new TelephonyInfo();

        TelephonyManager telephonyManager = ((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE));

        telephonyInfo.imeiSIM1 = telephonyManager.getDeviceId();;
        telephonyInfo.imeiSIM2 = null;

        try {
            telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceIdGemini", 0);
            telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceIdGemini", 1);
        } catch (GeminiMethodNotFoundException e) {
            e.printStackTrace();

            try {
                telephonyInfo.imeiSIM1 = getDeviceIdBySlot(context, "getDeviceId", 0);
                telephonyInfo.imeiSIM2 = getDeviceIdBySlot(context, "getDeviceId", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }

        telephonyInfo.isSIM1Ready = telephonyManager.getSimState() == TelephonyManager.SIM_STATE_READY;
        telephonyInfo.isSIM2Ready = false;

        try {
            telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimStateGemini", 0);
            telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimStateGemini", 1);
        } catch (GeminiMethodNotFoundException e) {

            e.printStackTrace();

            try {
                telephonyInfo.isSIM1Ready = getSIMStateBySlot(context, "getSimState", 0);
                telephonyInfo.isSIM2Ready = getSIMStateBySlot(context, "getSimState", 1);
            } catch (GeminiMethodNotFoundException e1) {
                //Call here for next manufacturer's predicted method name if you wish
                e1.printStackTrace();
            }
        }
    }

    return telephonyInfo;
}

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

    String imei = 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){
            imei = ob_phone.toString();

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

    return imei;
}

private static  boolean getSIMStateBySlot(Context context, String predictedMethodName, int slotID) throws GeminiMethodNotFoundException {

    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 getSimStateGemini = telephonyClass.getMethod(predictedMethodName, parameter);

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

        if(ob_phone != null){
            int simState = Integer.parseInt(ob_phone.toString());
            if(simState == TelephonyManager.SIM_STATE_READY){
                isReady = true;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new GeminiMethodNotFoundException(predictedMethodName);
    }

    return isReady;
}


private static class GeminiMethodNotFoundException extends Exception {

    private static final long serialVersionUID = -996812356902545308L;

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

然后按照以下方式使用这个类:

 
private void isDualSimOrNot(){
    TelephonyInfo telephonyInfo = TelephonyInfo.getInstance(this);

    String imeiSIM1 = telephonyInfo.getImeiSIM1();
    String imeiSIM2 = telephonyInfo.getImeiSIM2();

    boolean isSIM1Ready = telephonyInfo.isSIM1Ready();
    boolean isSIM2Ready = telephonyInfo.isSIM2Ready();

    boolean isDualSIM = telephonyInfo.isDualSIM();
    Log.i("Dual = "," IME1 : " + imeiSIM1 + "\n" +
            " IME2 : " + imeiSIM2 + "\n" +
            " IS DUAL SIM : " + isDualSIM + "\n" +
            " IS SIM1 READY : " + isSIM1Ready + "\n" +
            " IS SIM2 READY : " + isSIM2Ready + "\n");
}

请在应用的AndroidManifest.xml文件中添加此权限:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

上面的代码对我来说完全有效。

你遗漏了<uses-permission android:name="android.permission.READ_PHONE_STATE" />。我已经更新了答案。 - Saran
2
非常好的答案,谢谢。只有一个问题,有没有办法从SIM卡中获取更多信息?具体来说,我想知道它属于哪个运营商。IMEI更多与设备相关而不是SIM卡。我是对的吗? - Alireza Ahmadi
1
不适用于三星Note 3 Neo(SM-N7502)双卡:否 - SalmanShariati
对于安卓5.1及以上设备,您可以使用Subscription Manager读取双sim卡信息。如果您正在处理低版本的设备,则可使用以下替代方法:getDeviceIdDS(int插槽号) getDeviceId(int插槽号)注意:在三星Neo中不存在getDeviceIdGemini方法...您需要使用getDeviceIdDS。 - user3131373
2
关于SIM卡号(IMSI)的问题,但回答的是IMEI,这两者是不同的。 - eugeneek

0

使用此代码片段从双卡设备获取Sim卡详情!

  //above 22
     if (Build.VERSION.SDK_INT > 22) {
            //for dual sim mobile
            SubscriptionManager localSubscriptionManager = SubscriptionManager.from(this);
    
            if (localSubscriptionManager.getActiveSubscriptionInfoCount() > 1) {
             //if there are two sims in dual sim mobile
                List localList = localSubscriptionManager.getActiveSubscriptionInfoList();
                SubscriptionInfo simInfo = (SubscriptionInfo) localList.get(0);
                SubscriptionInfo simInfo1 = (SubscriptionInfo) localList.get(1);
    
                final String sim1 = simInfo.getDisplayName().toString();
                final String sim2 = simInfo1.getDisplayName().toString();
    
            }else{
             //if there is 1 sim in dual sim mobile
                TelephonyManager tManager = (TelephonyManager) getBaseContext()
                        .getSystemService(Context.TELEPHONY_SERVICE);
    
                String sim1 = tManager.getNetworkOperatorName();
    
            }
    
        }else{
            //below android version 22
                    TelephonyManager tManager = (TelephonyManager) getBaseContext()
                            .getSystemService(Context.TELEPHONY_SERVICE);
    
                    String sim1 = tManager.getNetworkOperatorName();
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接