Example usage for android.net ConnectivityManager getClass

List of usage examples for android.net ConnectivityManager getClass

Introduction

In this page you can find the example usage for android.net ConnectivityManager getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

/**
 * Check mobile data is enable or not./*from w  ww. j a  v a  2 s . co m*/
 * @param context
 * @return
 */
public static boolean isMobileDataEnable(Context context) {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        // Some problem accessible private API
        e.printStackTrace();
    }
    return mobileDataEnabled;
}

From source file:Main.java

/**
 * This method is used to check whether mobile data connection is enabled or disabled.
 * @param appContext//from  www .  j  a va 2  s.  c  o  m
 * @return
 */
public static boolean isMobileDataEnables(Context appContext) {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) appContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method call able
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        System.err.println("isMobileDataEnables() : " + e);
    }
    return mobileDataEnabled;
}

From source file:Main.java

/**
 * Enable/Disable mobile data./*from   w  w w . ja v a2s. c  o m*/
 * @param context
 * @param enabled
 */
public static void setMobileDataEnabled(Context context, boolean enabled) {
    try {
        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);
        // Method call for enabling mobile data..
        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
    } 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();
    }
}

From source file:Main.java

/**
 * Requires {@link android.Manifest.permission#READ_PHONE_STATE} permission.
 *
 * @param context/*  w ww.jav  a2 s .c o  m*/
 * @return
 */
public static boolean isMobileDataEnabled(Context context) {
    boolean mobileDataEnabled = false; // Assume disabled

    if (Build.VERSION.SDK_INT >= 21) {
        try {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            Class<?> tmClass = Class.forName(tm.getClass().getName());
            Method method = tmClass.getDeclaredMethod("getDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(tm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    } else {
        try {
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            Class<?> cmClass = Class.forName(cm.getClass().getName());
            Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
            method.setAccessible(true);
            mobileDataEnabled = (Boolean) method.invoke(cm);
        } catch (ClassNotFoundException | NoSuchMethodException | IllegalArgumentException
                | IllegalAccessException | InvocationTargetException ex) {
            ex.printStackTrace();
        }
    }

    return mobileDataEnabled;
}

From source file:Main.java

public static void setDataEnabled(Context context, boolean enabled) {
    ConnectivityManager conMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Class<?> conMgrClass = null;
    Field iConMgrField = null;//w ww  .  java 2s. c  o  m
    Object iConMgr = null;
    Class<?> iConMgrClass = null;
    Method setMobileDataEnabledMethod = null;
    try {
        conMgrClass = Class.forName(conMgr.getClass().getName());
        iConMgrField = conMgrClass.getDeclaredField("mService");
        iConMgrField.setAccessible(true);
        iConMgr = iConMgrField.get(conMgr);
        iConMgrClass = Class.forName(iConMgr.getClass().getName());
        setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);
        setMobileDataEnabledMethod.invoke(iConMgr, enabled);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.tingtingapps.securesms.mms.LegacyMmsConnection.java

@SuppressWarnings("TryWithIdenticalCatches")
protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }/* w w w  .ja v  a2  s. c o  m*/
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }

    if (inetAddress == null) {
        throw new IOException("Unable to lookup host: InetAddress.getByName() returned null.");
    }

    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null) {
        Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway.");
        return true;
    }

    Log.w(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    try {
        final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress",
                Integer.TYPE, InetAddress.class);
        final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager,
                MmsRadio.TYPE_MOBILE_MMS, inetAddress);
        Log.w(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained);
        return routeToHostObtained;
    } catch (NoSuchMethodException nsme) {
        Log.w(TAG, nsme);
    } catch (IllegalAccessException iae) {
        Log.w(TAG, iae);
    } catch (InvocationTargetException ite) {
        Log.w(TAG, ite);
    }

    final int ipAddress = Conversions.byteArrayToIntLittleEndian(ipAddressBytes, 0);
    final boolean routeToHostObtained = manager.requestRouteToHost(MmsRadio.TYPE_MOBILE_MMS, ipAddress);
    Log.w(TAG, "requestRouteToHost(" + ipAddress + ") -> " + routeToHostObtained);
    return routeToHostObtained;
}

From source file:org.thoughtcrime.securesms.mms.LegacyMmsConnection.java

@SuppressWarnings("TryWithIdenticalCatches")
protected static boolean checkRouteToHost(Context context, String host, boolean usingMmsRadio)
        throws IOException {
    InetAddress inetAddress = InetAddress.getByName(host);
    if (!usingMmsRadio) {
        if (inetAddress.isSiteLocalAddress()) {
            throw new IOException("RFC1918 address in non-MMS radio situation!");
        }//from  w  w  w .j  a v a  2  s  .  co m
        Log.w(TAG, "returning vacuous success since MMS radio is not in use");
        return true;
    }

    if (inetAddress == null) {
        throw new IOException("Unable to lookup host: InetAddress.getByName() returned null.");
    }

    byte[] ipAddressBytes = inetAddress.getAddress();
    if (ipAddressBytes == null) {
        Log.w(TAG, "resolved IP address bytes are null, returning true to attempt a connection anyway.");
        return true;
    }

    Log.i(TAG, "Checking route to address: " + host + ", " + inetAddress.getHostAddress());
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        final Method requestRouteMethod = manager.getClass().getMethod("requestRouteToHostAddress",
                Integer.TYPE, InetAddress.class);
        final boolean routeToHostObtained = (Boolean) requestRouteMethod.invoke(manager,
                MmsRadio.TYPE_MOBILE_MMS, inetAddress);
        Log.i(TAG, "requestRouteToHostAddress(" + inetAddress + ") -> " + routeToHostObtained);
        return routeToHostObtained;
    } catch (NoSuchMethodException nsme) {
        Log.w(TAG, nsme);
    } catch (IllegalAccessException iae) {
        Log.w(TAG, iae);
    } catch (InvocationTargetException ite) {
        Log.w(TAG, ite);
    }

    return false;
}

From source file:com.theonespy.util.Util.java

public static Boolean isMobileDataEnabled() {
    boolean mobileDataEnabled = false; // Assume disabled
    ConnectivityManager cm = (ConnectivityManager) MyApplication.getAppContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    try {/*from   w w w. j  av  a 2s  .co m*/
        Class cmClass = Class.forName(cm.getClass().getName());
        Method method = cmClass.getDeclaredMethod("getMobileDataEnabled");
        method.setAccessible(true); // Make the method callable
        // get the setting for "mobile data"
        mobileDataEnabled = (Boolean) method.invoke(cm);
    } catch (Exception e) {
        Util.Log("Can't check if mobile data is enabled: " + e);
    }

    return mobileDataEnabled;
}

From source file:org.hansel.myAlert.PanicButtonFragment.java

@SuppressWarnings("rawtypes")
private void setMobileDataEnabled(Context context, boolean enabled)
        throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, NoSuchMethodException,
        NoSuchFieldException, InvocationTargetException {
    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());
    @SuppressWarnings("unchecked")
    final Method setMobileDataEnabledMethod = iConnectivityManagerClass
            .getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
    setMobileDataEnabledMethod.setAccessible(true);

    setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
}

From source file:org.gdg.frisbee.android.widget.FeedbackFragment.java

private void buildProperties() {
    addProperty(PROPERTY_MODEL, Build.MODEL);
    addProperty(PROPERTY_ANDROID_VERSION, VERSION.RELEASE);

    try {/*  w w w  .  j a  v a  2  s  . c  o m*/
        WifiManager cm = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
        WifiInfo e = cm.getConnectionInfo();
        SupplicantState mobileDataEnabled = e.getSupplicantState();
        addProperty(PROPERTY_WI_FI_ENABLED, mobileDataEnabled);
    } catch (Exception e) {
        Timber.d(e, "Wifi Manager problem.");
    }

    boolean mobileDataEnabled1 = false;
    ConnectivityManager cm1 = (ConnectivityManager) getActivity()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    try {
        Class e1 = Class.forName(cm1.getClass().getName());
        Method resolution = e1.getDeclaredMethod("getMobileDataEnabled");
        resolution.setAccessible(true);
        mobileDataEnabled1 = (Boolean) resolution.invoke(cm1);
    } catch (Exception e) {
        Timber.d(e, "Mobil data problem.");
    }

    addProperty(PROPERTY_MOBILE_DATA_ENABLED, mobileDataEnabled1);

    try {
        LocationManager e2 = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        boolean resolution1 = e2.isProviderEnabled("gps");
        addProperty(PROPERTY_GPS_ENABLED, resolution1);
    } catch (Exception e) {
        Timber.d(e, "GPS problem.");
    }

    try {
        DisplayMetrics e3 = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(e3);
        String resolution2 = Integer.toString(e3.widthPixels) + "x" + Integer.toString(e3.heightPixels);
        addProperty(PROPERTY_SCREEN_RESOLUTION, resolution2);
    } catch (Exception e) {
        Timber.d(e, "Screen density problem.");
    }

    try {
        String e4 = getActivity().getClass().getSimpleName();
        addProperty(PROPERTY_ACTIVITY, e4);
    } catch (Exception e) {
        Timber.d(e, "Activity name problem.");
    }

    PackageManager manager = getActivity().getPackageManager();
    try {
        PackageInfo e = manager.getPackageInfo(getActivity().getPackageName(), 0);
        addProperty(PROPERTY_APP_VERSION_NAME, e.versionName);
        addProperty(PROPERTY_APP_VERSION_CODE, e.versionCode);
    } catch (PackageManager.NameNotFoundException var7) {
        Timber.d("Problem with PackageManager");
    }
}