检查是否可以通过 WiFi 或移动数据连接互联网
Checking if internet connection over WiFi or mobile data available
我正在使用以下代码检查 phone 是否已连接到互联网(WiFi 或移动数据):
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
}
} else {
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return (activeNetworkInfo != null) && activeNetworkInfo.isConnected();
}
可能 phone 已连接到 WiFi 但未连接到互联网。对于我的用例,我假设当它连接到 WiFi 或移动网络(启用移动数据)时,互联网将可用。
这段代码可以吗?特别是,activeNetworkInfo.isConnected()
是否也测试移动数据或仅测试 WiFi?我的目标是 Android 7 到 Android 10。理论上我可以将 if 子句中的代码片段也用于我认为 Android 10 以下的设备。
首先向 manifest.xml 请求此权限:android.permission.ACCESS_NETWORK_STATE
在android中,我们可以判断网络连接
通过使用 WIFIMANAGER 轻松获得状态。
private boolean LookForWifiOnAndConnected() {
WifiManager wifi_m = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
if (wifi_m.isWifiEnabled()) { // if user opened wifi
WifiInfo wifi_i = wifi_m.getConnectionInfo();
if( wifi_i.getNetworkId() == -1 ){
return false; // Not connected to any wifi device
}
return true; // Connected to some wifi device
}
else {
return false; // user turned off wifi
}
}
// 检查移动数据是否打开或关闭
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
}
我正在使用以下代码检查 phone 是否已连接到互联网(WiFi 或移动数据):
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
if (capabilities != null) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
return true;
} else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
return true;
} else return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
}
} else {
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return (activeNetworkInfo != null) && activeNetworkInfo.isConnected();
}
可能 phone 已连接到 WiFi 但未连接到互联网。对于我的用例,我假设当它连接到 WiFi 或移动网络(启用移动数据)时,互联网将可用。
这段代码可以吗?特别是,activeNetworkInfo.isConnected()
是否也测试移动数据或仅测试 WiFi?我的目标是 Android 7 到 Android 10。理论上我可以将 if 子句中的代码片段也用于我认为 Android 10 以下的设备。
首先向 manifest.xml 请求此权限:android.permission.ACCESS_NETWORK_STATE
在android中,我们可以判断网络连接 通过使用 WIFIMANAGER 轻松获得状态。
private boolean LookForWifiOnAndConnected() {
WifiManager wifi_m = (WifiManager)
getSystemService(Context.WIFI_SERVICE);
if (wifi_m.isWifiEnabled()) { // if user opened wifi
WifiInfo wifi_i = wifi_m.getConnectionInfo();
if( wifi_i.getNetworkId() == -1 ){
return false; // Not connected to any wifi device
}
return true; // Connected to some wifi device
}
else {
return false; // user turned off wifi
}
}
// 检查移动数据是否打开或关闭
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
}