How to know which SIM is consuming mobile data in dual SIM android phone?
Asked Answered
W

1

7

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.

Using below code I am able to know if my dual sim phone is connected to Wifi or mobile data.

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?

I had searched a lot and I saw many question posted in SO without answer like this.

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.

Many other application are able to do this like Mubble.

Can any one provide me a solution for it?

Weisburgh answered 17/3, 2017 at 8:29 Comment(0)
T
1

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.

After api level 24, there is a public system api android.telephony.SubscriptionManager#getDefaultDataSubscriptionId to get current active data sim subscription index.

Then, you can create a android.telephony.TelephonyManager or android.telephony.SubscriptionManager#getActiveSubscriptionInfo from subscription index to obtain sim operator information.

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;
    }
Tomokotomorrow answered 12/6, 2020 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.