set Mobile Data Disabled via - Android android.net

Android examples for android.net:ConnectivityManager

Description

set Mobile Data Disabled via

Demo Code

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.content.Context;
import android.net.ConnectivityManager;

public class Main {

  @SuppressWarnings({ "rawtypes", "unchecked" })
  public static void setMobileDataDisabled(Context context) 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);/*  w ww  .  j  a va2 s .  com*/
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(connectivityManager, false);
  }

}

Related Tutorials