Search code examples
androiddual-simdata-connections

disable data connection in dual sim android


I want to disable data connection and I am using this code.

ConnectivityManager dataManager;
    dataManager  = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        dataMtd = ConnectivityManager.class.getDeclaredMethod("setMobileDataEnabled", boolean.class);
    } catch (NoSuchMethodException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    dataMtd.setAccessible(true);
    try {
        dataMtd.invoke(dataManager, false);
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

this will not work in dual sim. so can someone help me. how can I disable data connection in dual sim android phone? and how can i check if phone is dual sim or not


Solution

  • Some things to mention here:

    • before Android 5.1 there was no official API for dual sim functionality. Thus there seems to be no universal solution for older devices. Since 5.1 an API is available.
    • your code will no longer work on Android 5+, as pointed out in this question
    • however, this answer provides a solution to both bullets above: the method setMobileNetworkfromLollipop checks whether target is 5 or 5.1+, and in case of 5.1+ it loops through all subscription id's (=sim cards) to switch data services. You could combine it with your code to target previous versions as well. The downside: it requires root access, and dual-sim functionality is limited to 5.1+.