简体   繁体   English

连接到Wifi时如何检查3G是否启用?

[英]How to check whether 3G is enabled when connected to Wifi?

I want to allow the user to toggle 3G state (ON/OFF) even when the device is connected to Wifi (I read here that disabling 3G while connected to Wifi saves battery). 我想允许用户即使将设备连接到Wifi也可以切换3G状态(开/关)(我在这里阅读在连接到Wifi时禁用3G可以节省电池)。

However, when I check the 3G state when the device is connected to Wifi it always return disconnected. 但是,当我将设备连接到Wifi时检查3G状态时,它始终会返回断开连接状态。

My code is: 我的代码是:

// access to mobile networtk service - 3G
ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mobile = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

// check mobile status
NetworkInfo.State state = mobile.getState(); // returns Disconnected 
Boolean isConnected = mobile.isConnectedOrConnecting(); // returns false

if (isConnected)
{
    // Turn 3G off
}
else
{
    // Turn 3G on
}

According what i know, When a wifi is connected the Mobile Network Connections are automatically disabled in Android, so if you check the status of 3G it will be disconnected only. 据我所知,当连接了wifi时,Android中的“移动网络连接”会自动禁用,因此,如果您检查3G的状态,则只能断开连接。 You don't need to do this programmatically, it is already present in Android System. 您无需以编程方式执行此操作,它已存在于Android System中。

Even though in the Navigation panel you may see 3G enabled, but it is not ON simultaneously with WiFi. 即使在“导航”面板中,您可能会看到已启用3G,但与WiFi同时未启用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM