Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 300
Hitesh Matnani Avatar asked Mar 17 '17 08:03

Hitesh Matnani


People also ask

How do I know which SIM is using data?

Go to Settings on your device. Go to Connections. Go to SIM Card Manager. Scroll down and check under Preferred SIM card which line is selected under Mobile Data.

How do I select data for my second SIM?

On the Home screen, tap Apps → Settings → SIM card manager → Select a SIM or USIM card and tap Register name or Select icon.

How does data usage work on Dual SIM?

How can I use mobile data in a dual SIM phone? While you can make and receive calls and SMS from either SIM card in dual SIM phones, you can only have one active data connection to the internet. In the Settings section of your phone you are required to set your preferred SIM for connecting to the Internet.

How do I change my mobile data from sim1 to sim2 on Android?

Go to Settings > Mobile network > SIM management and set SIM 1 or SIM 2 as the default mobile data or default calling SIM.


1 Answers

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;
    }
like image 195
alijandro Avatar answered Oct 04 '22 03:10

alijandro