Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I need something like ACCESS_WIFI_STATE but so I can manipulate with dataTrafiic through 3g

On my android I use WIFI and 3gdata traffic how to toggle off and on 3g data because it is not unlimited... I need just a class/function line of code that will show me TelephonyManager.DATA_TRAFIC is enable = false;

like image 433
Tony Avatar asked Jan 29 '26 14:01

Tony


1 Answers

It depends what android version you are running. Take a look at my code for a widget I developed.

The line of code you need is

TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
        isEnabled = true;
    } else {
        isEnabled = false;
    }

The "legacy" method is for pre-2.2 android version which uses TelephonyManager.

// 2.2+
private 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 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);
}

public static boolean isMobileDataConnected(Context context) {
    try {
        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("getMobileDataEnabled");
        setMobileDataEnabledMethod.setAccessible(true);

        return Boolean.valueOf(setMobileDataEnabledMethod.invoke(iConnectivityManager).toString());
    } catch (Exception e) {
        Log.e("NetSwitcher", e.toString());
    }
    return false;
}

// pre 2.2
public static void setMobileDataEnabledLegacy(Context context) {
    boolean isEnabled = false;
    Method dataConnSwitchmethod;
    Class telephonyManagerClass;
    Object ITelephonyStub;
    Class ITelephonyClass;

    TelephonyManager telephonyManager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

    if (telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED) {
        isEnabled = true;
    } else {
        isEnabled = false;
    }
    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());

        if (isEnabled) {
            dataConnSwitchmethod = ITelephonyClass
                    .getDeclaredMethod("disableDataConnectivity");
        } else {
            dataConnSwitchmethod = ITelephonyClass
                    .getDeclaredMethod("enableDataConnectivity");
        }
        dataConnSwitchmethod.setAccessible(true);
        dataConnSwitchmethod.invoke(ITelephonyStub);
    } catch (Exception e) {
        Log.e("NetSwitcher", e.toString());
    }
}
like image 87
Tomislav Markovski Avatar answered Feb 01 '26 05:02

Tomislav Markovski