title: Enable/Disable Data mobile programmatically - NO ROOT
body: <p>I'm trying to <code>enable/disable</code> data mobile in specific moments. I've tried it on <code>MotoG 4.4.2 (Rooted)</code> with it I had no problem to do this, but when I've tried it on <code>Samsung Galaxy Grand Prime 4.4.4 (No Root)</code> it doesn't work, also I've tried it on <code>Samsung Galaxy S3 4.3 (No Root)</code> but it worked.</p>
  
  <ol>
  <li>MotoG Worked -- <strong>WHY?</strong> -- ROOT Acces</li>
  <li>Samsung Galaxy Grand Prime didn't work -- <strong>WHY?</strong> -- I think it doesn't work because when I try to do it manually I've to accept a dialog that I want to lose data connectivity, and I think this is the problem because it doesn't work.</li>
  <li>Samsung Galaxy S3 worked -- <strong>WHY?</strong> -- I think it worked because the user put "Don't show me again" and user accepted it before.</li>
  </ol>
  
  <p>The code that I'm trying is : </p>
  
  <pre><code>public void GetDataConnectionAPI() {
      this.context.getApplicationContext();
      TelephonyManager telephonyManager =
              (TelephonyManager) this.context.getApplicationContext().
                      getSystemService(Context.TELEPHONY_SERVICE);
      try {
          telephonyManagerClass = Class.forName(telephonyManager.getClass().getName());
          Method getITelephonyMethod = telephonyManagerClass.getDeclaredMethod("getITelephony");
          getITelephonyMethod.setAccessible(true);
          ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
          ITelephonyClass = Class.forName(ITelephonyStub.getClass().getName());
  
          dataConnSwitchmethod_OFF =
                  ITelephonyClass.getDeclaredMethod("disableDataConnectivity");
          dataConnSwitchmethod_ON = ITelephonyClass.getDeclaredMethod("enableDataConnectivity");
      } catch (Exception e) { 
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>And then I enable/disable the data with this method : </p>
  
  <pre><code>public 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>
  
  <p>When I want to Enable I call this method : </p>
  
  <pre><code>public void EnableMobileData(){
      try {
          setMobileDataEnabled(context, true);
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      } catch (NoSuchFieldException e) {
          e.printStackTrace();
      } catch (IllegalAccessException e) {
          e.printStackTrace();
      } catch (NoSuchMethodException e) {
          e.printStackTrace();
      } catch (InvocationTargetException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>On my Manifest I put </p>
  
  <pre><code>&lt;!-- 3g uses-permisions--&gt;
  &lt;uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;
  &lt;uses-permission android:name="android.permission.MODIFY_PHONE_STATE" /&gt;
  </code></pre>
  
  <p>I wonder if it's any possibility to avoid this dialog or just accept it programmatically..</p>
  
  <p>I've read <a href="https://stackoverflow.com/a/14536363/4385913">Bojan Kogoj answer</a> that sais that I've to : </p>
  
  <blockquote>
    <ul>
    <li>Pre-installed into a system folder on the ROM</li>
    <li>Compiled by a manufacturer using their security certificate</li>
    </ul>
  </blockquote>
  
  <p>But I'm wondering if is any other way to do this.</p>