Android Network State Check setMobileDataEnabled(Activity a, Context context, boolean enabled)

Here you can find the source of setMobileDataEnabled(Activity a, Context context, boolean enabled)

Description

set Mobile Data Enabled

License

Open Source License

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void setMobileDataEnabled(Activity a, Context context,
        boolean enabled) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import android.app.Activity;

import android.content.Context;

import android.net.ConnectivityManager;

public class Main {

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static void setMobileDataEnabled(Activity a, Context context,
            boolean enabled) {
        final ConnectivityManager conman = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        Class conmanClass = null;
        try {//from w w  w  .ja  v a2 s  .  c  o  m
            conmanClass = Class.forName(conman.getClass().getName());
        } catch (ClassNotFoundException e) {
        }
        Field iConnectivityManagerField = null;
        try {
            iConnectivityManagerField = conmanClass
                    .getDeclaredField("mService");
        } catch (SecurityException e) {
        } catch (NoSuchFieldException e) {
        }
        iConnectivityManagerField.setAccessible(true);
        Object iConnectivityManager = null;
        try {
            iConnectivityManager = iConnectivityManagerField.get(conman);
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        }
        Class iConnectivityManagerClass = null;
        try {
            iConnectivityManagerClass = Class.forName(iConnectivityManager
                    .getClass().getName());
        } catch (ClassNotFoundException e) {
        }
        Method setMobileDataEnabledMethod = null;
        try {
            setMobileDataEnabledMethod = iConnectivityManagerClass
                    .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        } catch (SecurityException e) {
        } catch (NoSuchMethodException e) {
        }
        setMobileDataEnabledMethod.setAccessible(true);

        try {
            setMobileDataEnabledMethod
                    .invoke(iConnectivityManager, enabled);
        } catch (IllegalArgumentException e) {
        } catch (IllegalAccessException e) {
        } catch (InvocationTargetException e) {
        }

    }
}

Related

  1. getNetworkState(Context context)
  2. isLocationEnabledNetwork(Context c)
  3. isOpenNetwork(Context context)
  4. loadImageFromNetwork(String photoUrl)
  5. IsInternetReachable()
  6. isNetworkAvailable(Activity mainActivity)
  7. isDeviceOnline(Activity activity)
  8. isConnected(final Activity act)
  9. isOnline(Activity activity)