disable Mobile Network - Android Network

Android examples for Network:Network Status

Description

disable Mobile Network

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.content.Context;
import android.net.ConnectivityManager;

public class Main {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private static void disableMobileNetWork(Context context,
            boolean enabled) {
        try {/* ww  w .  ja  va2  s .com*/
            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(System.out);
        }
    }
}

Related Tutorials