如何知道双卡Android手机中哪张SIM卡在使用移动数据?

7

我正在开发一款网络监控应用。目前,我已成功实现了从wifi或移动数据跟踪数据使用情况等所有功能,但我想知道哪张SIM卡连接到了互联网并消耗了移动数据。

使用下面的代码,我可以知道我的双卡手机是否连接到了wifi或移动数据。

public static String isInternetConnected (Context ctx) {
    ConnectivityManager connectivityMgr = (ConnectivityManager) ctx
            .getSystemService(CONNECTIVITY_SERVICE);
    NetworkInfo wifi = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    NetworkInfo mobile = connectivityMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    // Check if wifi or mobile network is available or not. If any of them is
    // available or connected then it will return true, otherwise false;
    if (wifi != null) {
        if (wifi.isConnected()) {
            return "wifi";
        }
    }
    if (mobile != null) {
        if (mobile.isConnected()) {
            return "mobile";
        }
    }
    return "none";
}

如何在双卡 Android 手机上获取使用移动数据的 SIM 索引或运营商名称?
我已经搜索了很多并看到了很多问题发布在 SO 上,但没有答案,例如这个
我能够在双卡手机中获取两个 SIM 的 subId,但是我遇到了一个问题,那就是不知道哪个 SIM 正在使用互联网。
许多其他应用程序都可以做到这一点,例如 Mubble。
有人能提供解决方案吗?
1个回答

1
在API 22之后,您可以使用反射来使用隐藏的系统API android.telephony.SubscriptionManager#getDefaultDataSubId 来获取当前活动数据SIM订阅索引。
在API 24之后,有一个公共的系统API android.telephony.SubscriptionManager#getDefaultDataSubscriptionId 可以获取当前活动数据SIM订阅索引。
然后,您可以创建一个android.telephony.TelephonyManager或android.telephony.SubscriptionManager#getActiveSubscriptionInfo从订阅索引中获取SIM运营商信息。
以下是获取双卡手机数据SIM运营商信息的简单解决方案。
    public static String getDataSimOperator(Context context) {
        if (context == null) {
            return null;
        }

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    int dataSubId = SubscriptionManager.getDefaultDataSubscriptionId();
                    TelephonyManager dataSimManager = tm.createForSubscriptionId(dataSubId);
                    return dataSimManager.getSimOperator();
                } else {
                    String operator = getDataSimOperatorBeforeN(context);
                    if (operator != null) {
                        return operator;
                    } else {
                        return tm.getSimOperator();
                    }
                }
            } else {
                return tm.getSimOperator();
            }
        }
        return null;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
    private static String getDataSimOperatorBeforeN(Context context) {
        if (context == null) {
            return null;
        }

        int dataSubId = -1;
        try {
            Method getDefaultDataSubId = SubscriptionManager.class.getDeclaredMethod("getDefaultDataSubId");
            if (getDefaultDataSubId != null) {
                getDefaultDataSubId.setAccessible(true);
                dataSubId = (int) getDefaultDataSubId.invoke(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (dataSubId != -1) {
            SubscriptionManager sm = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
            if (sm != null && ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE)
                    == PackageManager.PERMISSION_GRANTED) {
                SubscriptionInfo si = sm.getActiveSubscriptionInfo(dataSubId);
                if (si != null) {
                    // format keep the same with android.telephony.TelephonyManager#getSimOperator
                    // MCC + MNC format
                    return String.valueOf(si.getMcc()) + si.getMnc();
                }
            }
        }
        return null;
    }

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