简体   繁体   English

在Android双卡手机上,如何检测哪个SIM卡启用了数据?

[英]On Android dual sim phone, how to detect which SIM card has data enabled?

I'm writing a network monitor app.我正在编写一个网络监视器应用程序。 I want to detect which SIM card in a dual SIM phone has data enabled?我想检测双卡手机中哪张 SIM 卡启用了数据功能?

I have tried TelephonyManager.isDataEnabled() methods for both SIM cards.我已经为两张 SIM 卡尝试了 TelephonyManager.isDataEnabled() 方法。 But the method returns enabled for both SIMs.但是该方法返回为两个 SIM 卡启用。 Instead I should be getting enabled for one SIM card and disabled for the other.相反,我应该为一张 SIM 卡启用并为另一张禁用。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {

    SubscriptionManager subManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    List<SubscriptionInfo> subscriptionInfoList = subManager.getActiveSubscriptionInfoList();

    TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        TelephonyManager mgr = telephonyManager.createForSubscriptionId(subscriptionInfoList.get(0).getSubscriptionId());
            int isDataEnabledSIM1 = mgr.isDataEnabled()?1:0;

            mgr = telephonyManager.createForSubscriptionId(subscriptionInfoList.get(1).getSubscriptionId());
            int isDataEnabledSIM2 = mgr.isDataEnabled()?1:0;

            Log.d(TAG, "isDataEnabledSIM1: "+isDataEnabledSIM1 + ", isDataEnabledSIM2="+isDataEnabledSIM2);
    }
}

Both isDataEnabledSIM1 and isDataEnabledSIM2 are returned as 1 indicating data is enabled on both SIMs. isDataEnabledSIM1 和 isDataEnabledSIM2 都返回为 1,表示在两个 SIM 卡上都启用了数据。 This is obviously incorrect, I am expecting 1 only for one SIM card and 0 for the other.这显然是不正确的,我只期望一张 SIM 卡为 1,另一张为 0。

You can use getDefaultDataSubscriptionId() from SubscriptionManager (For N and higher) and under N you can use reflection.您可以使用 SubscriptionManager 中的 getDefaultDataSubscriptionId()(对于 N 及更高版本),并且在 N 下您可以使用反射。 Example:例子:

public static int getDefaultDataSubscriptionId(Context context) {
    SubscriptionManager subscriptionManager = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
        subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
    }
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        int nDataSubscriptionId = SubscriptionManager.getDefaultDataSubscriptionId();

        if (nDataSubscriptionId != SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
            return (nDataSubscriptionId);
        }
    }

    try {
        Class<?> subscriptionClass = Class.forName(subscriptionManager.getClass().getName());
        try {
            Method getDefaultDataSubscriptionId = subscriptionClass.getMethod("getDefaultDataSubId");

            try {
                return ((int) getDefaultDataSubscriptionId.invoke(subscriptionManager));
            } catch (IllegalAccessException e) {
                return ERROR_CODE_EXCEPTION_HANDLED;
            } catch (InvocationTargetException e) {
                return ERROR_CODE_EXCEPTION_HANDLED;
            }
        } catch (NoSuchMethodException e) {
            return ERROR_CODE_NO_SUCH_METHOD;
        }
    } catch (ClassNotFoundException e) {
        return ERROR_CODE_EXCEPTION_HANDLED;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM