如何判断“移动网络数据”启用或禁用(通过无线连接,即使)?如何判断、无线连接、数据、网络

2023-09-12 01:44:45 作者:空城旧梦*

我有,我希望能够用它来从一​​个远程查询的连接状态报告的应用程序。

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

我想知道,如果无线网络连接,如果数据访问启用了移动网络。

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.

问题是,数据启用,当我通过无线网络连接总是返回为真,我只能正确查询移动网络时,通过无线网络没有连接。

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.

所有我见过的答案建议投票查看当前连接的是什么,但我想知道,如果移动网络可用,我应该需要它,即使我可以通过无线网络在present连接。

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?

修改

因此​​,通过无线网络连接。如果我去设置,取消选择数据已启用,然后在我的应用程序,当我做到这一点:

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返回为真,但我有禁用移动网络数据,所以我会想到它是假

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.

让我怎么检查,如果移动网络数据被启用时,我通过无线连接?

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

更新

我看了看getAllNetworkInfo()由ss1271提出的意见

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

我输出以下三个条件下返回有关移动网络的信息

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关闭:

移动[HSUPA],状态:连接/关连,原因是:未知,多余的:   互联网,漫游:假故障:假的,isAvailable:真正的,   FEATUREID:-1,userDefault:假

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

随着无线开/关移动

的NetworkInfo:类型:移动[HSUPA],状态:DISCONNECTED /断开,   原因:connectionDisabled,多余的:(无),漫游:假的,   故障:假的,isAvailable:真,FEATUREID:-1,userDefault:   假

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

随着无线开/移动在

的NetworkInfo:类型:移动[HSPA],状态:DISCONNECTED /断开,   原因:connectionDisabled,多余的:(无),漫游:假的,   故障:假的,isAvailable:真,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返回真实的,国家只表现为断开时的WiFi于影响。

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

推荐答案

以下code会告诉你,如果移动数据已启用与否,不管是否有一个移动数据连接活跃在那一刻,或无论是否wifi的启用/激活。这code只能在Android 2.3(姜饼),后来。 其实这code也适用于早期版本的Andr​​oid,以及; - )

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 才能够使用这个code

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