title: Toggle mobile data programmatically
body: <p>I have a method to enable or disable the mobile data</p>
  
  <pre><code>public void toggleMobileData(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 (Exception e){
              e.printStackTrace();
              Toast.makeText(context.getApplicationContext(),context.getResources().getString(R.string.no_change_ntework),Toast.LENGTH_SHORT).show();
          }
      }
  </code></pre>
  
  <p>It works fine, but i'm trying it with a Samsung S5 and I'm getting this error:</p>
  
  <pre><code>02-13 17:00:59.680    1928-1928/com.app.innergy W/System.err﹕ java.lang.NoSuchMethodException: setMobileDataEnabled [boolean]
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.Class.getDeclaredMethod(Class.java:635)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.app.innergy.SystemManager.toggleMobileData(SystemManager.java:122)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.app.innergy.SingleOptionAdapter$2.onClick(SingleOptionAdapter.java:138)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.view.View.performClick(View.java:5162)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.widget.CompoundButton.performClick(CompoundButton.java:128)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.view.View$PerformClick.run(View.java:20873)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:739)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.os.Looper.loop(Looper.java:145)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5834)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
  02-13 17:00:59.690    1928-1928/com.app.innergy W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
  </code></pre>
  
  <p>EDIT: This is where I call the method (is from an adapter of a custom listview in a fragment, i'm not posting all the code because it would be too long I guess)</p>
  
  <pre><code>//...
  
  if (soa.getTitle().equals(activity.getResources().getString(R.string.mobile_data))) {
                              fileContent = !soa.isToggle() ? activity.getResources().getString(R.string.activated) : activity.getResources().getString(R.string.deactivated);
                              systemManager.toggleMobileData(!soa.isToggle());
                              soa.setToggle(!soa.isToggle());
                              fileManager.createFile();
                              fileManager.writeToFile(activity.getResources().getString(R.string.mobile_data)+": "+ fileContent);
  //...
  </code></pre>