简体   繁体   English

如何知道哪个SIM卡在双卡Android手机中消耗移动数据?

[英]How to know which SIM is consuming mobile data in dual SIM android phone?

I am building a network monitor app.我正在构建一个网络监控应用程序。 Here I have successfully implemented all the things like track data usage from Wifi or mobile data, but I want to know which SIM is connected to internet and consuming mobile data.在这里,我已经成功地实现了所有的事情,比如从 Wifi 或移动数据跟踪数据使用情况,但我想知道哪个 SIM 卡连接到互联网并使用移动数据。

Using below code I am able to know if my dual sim phone is connected to Wifi or mobile data.使用下面的代码,我可以知道我的双卡手机是否连接到 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";
}

How can I get SIM Index or sim operator name that is consuming mobile data in dual sim android phone?如何在双卡安卓手机中获取消耗移动数据的 SIM 索引或 SIM 运营商名称?

I had searched a lot and I saw many question posted in SO without answer like this .我已经搜查了很多,我看到了许多问题张贴在SO不喜欢回答这个

I am able to get subId of both SIM in dual SIM phone but I am phasing problem to know which SIM is using internet.我能够在双 SIM 卡手机中获得两个 SIM 卡的 subId,但我正在逐步解决问题以了解哪个 SIM 卡正在使用互联网。

Many other application are able to do this like Mubble.许多其他应用程序能够像 Mubble 一样做到这一点。

Can any one provide me a solution for it?任何人都可以为我提供解决方案吗?

After api level 22, you can use the hidden system api android.telephony.SubscriptionManager#getDefaultDataSubId via reflection to get current active data sim subscription index. api level 22之后,你可以通过反射使用隐藏的系统api android.telephony.SubscriptionManager#getDefaultDataSubId来获取当前活动的数据sim订阅索引。

After api level 24, there is a public system api android.telephony.SubscriptionManager#getDefaultDataSubscriptionId to get current active data sim subscription index.在api level 24之后,有一个公共系统api android.telephony.SubscriptionManager#getDefaultDataSubscriptionId来获取当前活动数据sim订阅索引。

Then, you can create a android.telephony.TelephonyManager or android.telephony.SubscriptionManager#getActiveSubscriptionInfo from subscription index to obtain sim operator information.然后,您可以从订阅索引创建一个android.telephony.TelephonyManagerandroid.telephony.SubscriptionManager#getActiveSubscriptionInfo来获取 sim 运营商信息。

Here is a simple solution to get data sim operator for dual sim phone.这是获取双卡手机数据卡运营商的简单解决方案。

    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;
    }

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

相关问题 在Android双卡手机上,如何检测哪个SIM卡启用了数据? - On Android dual sim phone, how to detect which SIM card has data enabled? Android:如何在双卡手机中获取两个SIM的SIM ID - Android : How to get SIM ID of both SIM in Dual SIM mobile 双卡双待安卓手机 - dual sim android phone which sim receive a call Android - 如何通过双卡手机中的特定卡拨打电话? - Android - How to place a call through specific sim in dual sim phone? 如何在双SIM卡Android手机中找到来电的目标SIM卡? - How to find target sim for an incoming call in dual sim android phone? Android USSD 哪个sim接收ussd消息或哪个sim插槽接收ussd消息(双卡手机) - Android USSD which sim receive a ussd message or which sim slot receives a ussd message (dual sim phone) 如何在双SIM卡Android手机中获取getSimOperator? - How to getSimOperator in dual SIM phone android? 如何在不调用所有双SIM卡Android手机上的系统SIM选择器的情况下直接拨打带有指定SIM卡的号码? - How to directly dial a number with specified sim without invoking system SIM chooser on all dual SIM android phone? Android 如何从双卡手机获取电话号码 - Android How to get Phone number from the Dual sim phone 从双SIM卡发送短信时,询问在Xamarin Android中发送短信的SIM卡是什么? - At the time of Send SMS from dual sim Phone ask Which sim to send sms in Xamarin Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM