如何在没有 Networkinfo(Java) 的情况下检查互联网
How to check internet without Networkinfo(Java)
由于 NetworkInfo
已弃用,我无法找到任何替代解决方案。
我试过 ConnectivityManager.NetworkCallbacks
但在这个 OnNetworkUnavailable
中不起作用。
单击按钮时如何检查互联网?
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
在清单中添加此权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
然后使用下面的方法
public boolean isOnline() {
boolean isConnected = false;
ConnectivityManager connectivityMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
// Checking internet connectivity
NetworkInfo activeNetwork = null;
if (connectivityMgr != null) {
activeNetwork = connectivityMgr.getActiveNetworkInfo(); // Deprecated in API 29
}
isConnected = activeNetwork != null;
} else {
Network[] allNetworks = connectivityMgr.getAllNetworks(); // added in API 21 (Lollipop)
for (Network network : allNetworks) {
NetworkCapabilities networkCapabilities = connectivityMgr.getNetworkCapabilities(network);
if (networkCapabilities != null) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET))
isConnected = true;
}
}
}
return isConnected;
}
fun hasActiveInternetConnection(): Boolean {
try {
val urlc: HttpURLConnection =
URL("http://www.google.com").openConnection() as HttpURLConnection
urlc.setRequestProperty("User-Agent", "Test")
urlc.setRequestProperty("Connection", "close")
urlc.connectTimeout = 1500
urlc.connect()
return urlc.responseCode == 200
} catch (e: Exception) {
}
return false
}
此方法return活动互联网连接不仅刚刚连接!
在java
Boolean hasActiveInternetConnection() {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
return false;
}
}
由于 NetworkInfo
已弃用,我无法找到任何替代解决方案。
我试过 ConnectivityManager.NetworkCallbacks
但在这个 OnNetworkUnavailable
中不起作用。
单击按钮时如何检查互联网?
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected();
}
在清单中添加此权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
然后使用下面的方法
public boolean isOnline() {
boolean isConnected = false;
ConnectivityManager connectivityMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
// Checking internet connectivity
NetworkInfo activeNetwork = null;
if (connectivityMgr != null) {
activeNetwork = connectivityMgr.getActiveNetworkInfo(); // Deprecated in API 29
}
isConnected = activeNetwork != null;
} else {
Network[] allNetworks = connectivityMgr.getAllNetworks(); // added in API 21 (Lollipop)
for (Network network : allNetworks) {
NetworkCapabilities networkCapabilities = connectivityMgr.getNetworkCapabilities(network);
if (networkCapabilities != null) {
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
|| networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET))
isConnected = true;
}
}
}
return isConnected;
}
fun hasActiveInternetConnection(): Boolean {
try {
val urlc: HttpURLConnection =
URL("http://www.google.com").openConnection() as HttpURLConnection
urlc.setRequestProperty("User-Agent", "Test")
urlc.setRequestProperty("Connection", "close")
urlc.connectTimeout = 1500
urlc.connect()
return urlc.responseCode == 200
} catch (e: Exception) {
}
return false
}
此方法return活动互联网连接不仅刚刚连接!
在java
Boolean hasActiveInternetConnection() {
try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
return false;
}
}