android - 我需要类似 ACCESS_WIFI_STATE 的东西,但这样我就可以通过 3g 处理数据流量

标签 android permissions android-wifi telephonymanager

在我的 Android 上,我使用 WIFI 和 3g 数据流量如何关闭和打开 3g 数据,因为它不是无限的......我只需要一个类/函数代码行,它会告诉我 TelephonyManager.DATA_TRAFIC is enable = false;

最佳答案

这取决于你运行的安卓版本。 Take a look at my code对于我开发的小部件。

你需要的代码行是

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

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

“传统”方法适用于使用 TelephonyManager 的 2.2 之前的 android 版本。

// 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());
    }
}

关于android - 我需要类似 ACCESS_WIFI_STATE 的东西,但这样我就可以通过 3g 处理数据流量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9416030/

相关文章:

javascript - 如何在 cordova 的相机顶部重叠 navigator.notification.prompt

linux - 授予权限后无法导航到 Ubuntu 中的/opt/lampp/htdocs

安卓权限: who is using what?

android - 两个 Wifi 设备之间的数据传输

Android:将主机名解析为 IP 时出错

android - 使用 Monodroid/Xamarin 绘制 Map 的空引用异常

android - 使用 SearchView 时未显示定义的菜单图标

android - 在 Gluon map 上显示工具提示 onclick MapPoint

mysql - 如何让一个 MySQL 用户只能访问一个模式

android - 相机wifi密码未存储,如何通过WiFi发现sony相机