Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to have the instance of IConnectivityManager class from the connectivityManager object and then use the setMobileDataEnabled() method on the iConnectivityManager instance.</p> <p>Try the code below (tested on Galaxy Y (2.3) and Nexus 4 (4.2)) :</p> <pre><code>public void cellularState(boolean shouldEnable) { ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); try { Class&lt;?&gt; conmanClass = Class.forName(conman.getClass().getName()); Field iConnectivityManagerField = conmanClass.getDeclaredField("mService"); iConnectivityManagerField.setAccessible(true); Object iConnectivityManager = iConnectivityManagerField.get(conman); Class&lt;?&gt; iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName()); Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE); setMobileDataEnabledMethod.setAccessible(true); setMobileDataEnabledMethod.invoke(iConnectivityManager, shouldEnable); } catch(IllegalAccessException e) { e.printStackTrace(); } 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 (NoSuchMethodException 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(); } </code></pre> <p>In manifest declare:</p> <p><code>&lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/&gt;</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload