title: Mobile data is not turning off programmatically
body: <p>I am using the following code repeatedly to toggle between the ON and OFF states in mobile data.</p>
  
  <pre><code>//Turning mobile data ON/OFF
  boolean mobileDataEnabled = false; // Assume disabled
  ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  try {
      Class cmClass = Class.forName(cm.getClass().getName());
      Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
      method.setAccessible(true); // Make the method callable
      // get the setting for "mobile data"
      mobileDataEnabled = (Boolean)method.invoke(cm);
      method.setAccessible(false);
  } catch (Exception e) {
      // Some problem accessible private API
      // TODO do whatever error handling you want here
      e.printStackTrace();
  }
  
  if(mobileDataEnabled)
  {
      try {
          System.out.println("Mobile data switching off");
          ConnectivityManager enableDataConn = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
          Class enableClass = Class.forName(enableDataConn.getClass().getName());
          java.lang.reflect.Field iConnectivityManagerField = enableClass.getDeclaredField("mService");
          iConnectivityManagerField.setAccessible(true);
          final Object iConnectivityManager = iConnectivityManagerField.get(enableDataConn);
          Method method2 = enableClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
          method2.setAccessible(true); // Make the method callable
          // get the setting for "mobile data"
          method2.invoke(enableDataConn, false);
      } catch (Exception e) {
          //  Toast.makeText(getApplicationContext(),"Mobile data cannot be enabled",Toast.LENGTH_LONG).show();
          e.printStackTrace();
      }
  } else {
      try {
          System.out.println("Mobile data switching on");
          ConnectivityManager enableDataConn = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
          Class enableClass = Class.forName(enableDataConn.getClass().getName());
          java.lang.reflect.Field iConnectivityManagerField = enableClass.getDeclaredField("mService");
          iConnectivityManagerField.setAccessible(true);
          final Object iConnectivityManager = iConnectivityManagerField.get(enableDataConn);
          Method method2 = enableClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
          method2.setAccessible(true); // Make the method callable
          // get the setting for "mobile data"
          method2.invoke(enableDataConn, true);
      } catch (Exception e) {
          //Toast.makeText(getApplicationContext(),"Mobile data cannot be enabled",Toast.LENGTH_LONG).show();
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The above mentioned code is kept inside the onclick method of an onClicklistener method.</p>
  
  <p>I have no problem turning <strong>ON</strong> the mobile data when it is OFF, but <strong>it does not switch off when I try to toggle again</strong>. Also I do not get any exception in this case.
  It always goes into the <strong>else</strong> condition of the code.</p>
  
  <p>Please let me know what changes I must make so that this works both ways.       </p>