检查手机是否正在使用WIFI或数据/3G [英] Check whether mobile is using WIFI or Data/3G
本文介绍了检查手机是否正在使用WIFI或数据/3G的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设设备上同时启用了WIFI和数据/3G,如何检查用户当前是否正在通过wifi使用互联网或数据计划(假设它们都已启用).因此,我不需要检查它们是启用还是禁用,我想检测用户实际使用的是哪个.
Assuming that both WIFI and Data/3G are enabled on a device, how do I check if the user is currently using the internet over the wifi or the data plan assuming they are both enabled. So I don't need to check if they are enabled or disabled, I would like to detect which one is the user actually using.
我一直在做以下事情,这是唯一的解决方法吗?
I've been doing the following, is this the only solution?
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()) == NetworkInfo.DetailedState.CONNECTED) {
String ssid = wifiInfo.getSSID();
}
推荐答案
void chkStatus() {
final ConnectivityManager connMgr = (ConnectivityManager)
this.getSystemService(Context.CONNECTIVITY_SERVICE);
final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (wifi.isConnectedOrConnecting ()) {
Toast.makeText(this, "Wifi", Toast.LENGTH_LONG).show();
} else if (mobile.isConnectedOrConnecting ()) {
Toast.makeText(this, "Mobile 3G ", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "No Network ", Toast.LENGTH_LONG).show();
}
}
这篇关于检查手机是否正在使用WIFI或数据/3G的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文