0

I am trying to change the network state of the device from the source code.
I observed that there is a permission to grant to the applications for changing the network state i.e., CHANGE_NETWORK_STATE.

But I am not able to find any example or code that can change the network state of a device.

Please let me know if anyone knows or have an example to change the network state of a device.

Regards,
SSuman185

Suman
  • 4,221
  • 7
  • 44
  • 64
  • I wrote about this subject on my personal blog. Check it out http://www.oguzozkeroglu.com/android-enable-disable-wifi/ (It is in Turkish language but you can understand the code) – Oguz Ozkeroglu Jan 23 '12 at 11:27

2 Answers2

3

This code is working. I got it from here.

 private void setMobileDataEnabled(Context context, boolean enabled) {

        try {
        final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
        iConnectivityManagerField.setAccessible(true);
        final Object iConnectivityManager = iConnectivityManagerField.get(conman);
        final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {

        }
    }
Community
  • 1
  • 1
Suman
  • 4,221
  • 7
  • 44
  • 64
  • What about a solution for enabling an app or widget that provides a big red switch for disabling and enabling cellular radio, not just mobile data but the 2g/3g voice & text messaging radio support as well? The scenario behind this ask is outlined here https://android.stackexchange.com/questions/213150/app-or-widget-or-other-solution-for-turning-just-cellular-radio-off-on and has to do with forcing use of wifi calling / voice [ & text ] over wifi while conserving battery power that would otherwise be expended by leaving cellular radio running when you not required during wifi connected times. – myusrn Jun 02 '19 at 06:07
1

you can change wifi state by

 WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
 wifiManager.setWifiEnabled(true);

here is an answer which you can look into click here

The Dataconnection disable and enabling APIS are hidden in the SDK and not exposed to the user, this can be achived by accessing the ITelephony interface using the java reflection technique.

here you go:

Method dataConnSwitchmethod;
Class telephonyManagerClass;
Object ITelephonyStub;
Class ITelephonyClass;

TelephonyManager telephonyManager = (TelephonyManager) context
        .getSystemService(Context.TELEPHONY_SERVICE);

if(telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED){
    isEnabled = true;
}else{
    isEnabled = false;  
}   

telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
getITelephonyMethod.setAccessible(true);
ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());

if (isEnabled) {
    dataConnSwitchmethod = ITelephonyClass
            .getDeclaredMethod("disableDataConnectivity");
} else {
    dataConnSwitchmethod = ITelephonyClass
            .getDeclaredMethod("enableDataConnectivity");   
}
dataConnSwitchmethod.setAccessible(true);
dataConnSwitchmethod.invoke(ITelephonyStub);
Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • Thanks for the info. Once we get the method, will you please let me know how to call those methods and also I found that this is no longer supported from the GingerBread (2.3) version [Link: http://stackoverflow.com/questions/4715250/how-to-grant-modify-phone-state-permission-for-apps-ran-on-gingerbread]. Is it true? – Suman Jan 23 '12 at 11:45
  • dataConnSwitchmethod.setAccessible(true); dataConnSwitchmethod.invoke(ITelephonyStub); – AAnkit Jan 23 '12 at 12:18
  • Thanks for the info. But its not working, I am getting the MODIFY_PHONE_STATE permission exception even though added the same in AndroidManifest.xml file. Voting for wifi. – Suman Jan 23 '12 at 12:27