title: Android L (5.x) Turn ON/OFF "Mobile Data" programmatically
body: <p>I need to Turn ON/OFF Mobile data programmatically. Below code is not working for 5.x. Can you please help me. Thanks in advance.</p>
  
  <pre><code>private void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
          final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
          final Class conmanClass = Class.forName(conman.getClass().getName());
          final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
          connectivityManagerField.setAccessible(true);
          final Object connectivityManager = connectivityManagerField.get(conman);
          final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
          final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
          setMobileDataEnabledMethod.setAccessible(true);
          setMobileDataEnabledMethod.invoke(connectivityManager, enabled);    }
  </code></pre>
  
  <blockquote>
    <p>03-30 12:42:29.466: W/System.err(5966):
    java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] 03-30
    12:42:29.466: W/System.err(5966):     at
    java.lang.Class.getMethod(Class.java:664) 03-30 12:42:29.466:
    W/System.err(5966):   at
    java.lang.Class.getDeclaredMethod(Class.java:626)</p>
  </blockquote>
  
  <p>java.lang.NoSuchMethodException: setMobileDataEnabled [boolean] @ below line.</p>
  
  <blockquote>
    <p>final Method setMobileDataEnabledMethod =
    connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled",
    Boolean.TYPE);</p>
  </blockquote>