如何判断“移动网络数据"是启用还是禁用(即使通过 WiFi 连接)? [英] How to tell if 'Mobile Network Data' is enabled or disabled (even when connected by WiFi)?

查看:26
本文介绍了如何判断“移动网络数据"是启用还是禁用(即使通过 WiFi 连接)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我希望能够使用它从远程查询中获取连接状态报告.

I have an app that I want to be able to use to get a connection status report from a remote query.

我想知道是否已连接 WiFi,以及是否启用了移动网络数据访问.

I want to know if WiFi is connected, and if data access is enabled over mobile network.

如果 WiFi 超出范围,我想知道我是否可以依赖移动网络.

If the WiFi goes out of range I want to know if I can rely on the mobile network.

问题是当我通过WiFi连接时,数据启用总是返回为true,而我只有在没有通过WiFi连接时才能正确查询移动网络.

The problem is that data enabled is always returned as true when I am connected by WiFi, and I can only properly query the mobile network when not connected by WiFi.

我看到的所有答案都建议轮询以查看当前连接是什么,但我想知道是否需要移动网络,即使我目前可能通过 WiFi 连接.

all the answers I have seen suggest polling to see what the current connection is, but I want to know if mobile network is available should I need it, even though I might be connected by WiFi at present.

有没有办法知道是否启用了移动网络数据,而无需轮询查看是否已连接?

Is there anyway of telling whether mobile network data is enabled without polling to see if is connected?

编辑

因此,当通过 WiFi 连接时,如果我转到设置并取消选择启用数据",然后在我的应用程序中执行以下操作:

So when connected by WiFi If I go to settings and deselect 'Data Enabled' and then in my app I do this:

 boolean mob_avail = 
 conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isAvailable();

mob_avail 返回为true",但我已禁用移动网络数据,因此我希望它为false"

mob_avail is returned as 'true', but I have disabled Mobile Network Data, so I would expect it to be 'false'

如果我关闭 WiFi,则(正确地)没有连接,因为我已禁用移动网络数据.

If I turn off the WiFi, there is (rightly) no connection as I have disabled mobile network data.

那么当我通过WiFi连接时如何检查移动网络数据是否启用?

so how do I check if mobile network data is enabled when I am connected by WiFi?

更新

我按照 ss1271 的评论中的建议查看了 getAllNetworkInfo()

I took a look at getAllNetworkInfo() as suggested in the comments by ss1271

我在以下3种情况下输出了返回的关于移动网络的信息

I outputted the info returned about the mobile network under the following 3 conditions

WiFi 关闭 - 移动数据开启

WiFi Off - Mobile Data on

WiFi 开启 - 移动数据关闭

WiFi On - Mobile Data off

WiFi 开启 - 移动数据开启

WiFi On - Mobile Data on

并得到以下结果:

WiFi 关闭:

mobile[HSUPA],状态:CONNECTED/CONNECTED,原因:未知,额外:互联网,漫游:假,故障转移:假,isAvailable:真,featureId: -1, userDefault: false

mobile[HSUPA], state: CONNECTED/CONNECTED, reason: unknown, extra: internet, roaming: false, failover: false, isAvailable: true, featureId: -1, userDefault: false

打开 WiFi/关闭移动设备

NetworkInfo:类型:mobile[HSUPA],状态:DISCONNECTED/DISCONNECTED,原因:connectionDisabled,额外:(无),漫游:假,故障转移:false,isAvailable:true,featureId:-1,userDefault:假

NetworkInfo: type: mobile[HSUPA], state: DISCONNECTED/DISCONNECTED, reason: connectionDisabled, extra: (none), roaming: false, failover: false, isAvailable: true, featureId: -1, userDefault: false

打开 WiFi/打开手机

NetworkInfo:类型:mobile[HSPA],状态:DISCONNECTED/DISCONNECTED,原因:connectionDisabled,额外:(无),漫游:假,故障转移:false,isAvailable:true,featureId:-1,userDefault:假

NetworkInfo: type: mobile[HSPA], state: DISCONNECTED/DISCONNECTED, reason: connectionDisabled, extra: (none), roaming: false, failover: false, isAvailable: true, featureId: -1, userDefault: false

如您所见,isAvailable 每次都返回 true,并且状态仅在 WiFi 有效时显示为 Disconnected.

So as you can see isAvailable returned true each time, and state only showed as Disconnected when WiFi was in affect.

澄清

不想查看我的手机当前是否通过移动网络连接.我AM 尝试确定用户是否已启用/禁用移动网络数据访问.他们可以通过转到设置 -> 无线和网络设置 -> 移动网络设置 -> 启用数据

I am NOT looking to see if my phone is currently connected by Mobile Network. I AM trying to establish whether or not the user has enabled / disabled Data access over mobile network. They can turn this on and off by going to Settings -> Wireless and Network Settings ->Mobile Network Settings -> Data enabled

推荐答案

以下代码将告诉您是否启用了移动数据",无论此时是否有移动数据连接处于活动状态或wifi 是否已启用/活动与否. 此代码仅适用于 Android 2.3 (Gingerbread) 及更高版本.实际上这段代码也适用于早期版本的Android ;-)

The following code will tell you if "mobile data" is enabled or not, regardless of whether or not there is a mobile data connection active at the moment or whether or not wifi is enabled/active or not. This code only works on Android 2.3 (Gingerbread) and later. Actually this code also works on earlier versions of Android as well ;-)

    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
        // TODO do whatever error handling you want here
    }

注意:您需要拥有 android.permission.ACCESS_NETWORK_STATE 权限才能使用此代码.

Note: you will need to have permission android.permission.ACCESS_NETWORK_STATE to be able to use this code.

这篇关于如何判断“移动网络数据"是启用还是禁用(即使通过 WiFi 连接)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆