该工具类用来实现同步判断网络的连接状态
在使用工具类之前,确保app打开了访问网络状态的权限,也就是在AndroidManifest.xml里注册权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
废话不多说直接上代码
public class NetworkUtils {
@SuppressWarnings("deprecation")
@RequiresPermission(ACCESS_NETWORK_STATE)
public static boolean iConnected(@NonNull Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
if (networkCapabilities != null) {
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
}
} else {
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
return false;
}
@SuppressWarnings("deprecation")
public static boolean isWifiConnected(@NonNull Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
if (networkCapabilities != null) {
return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
}
} else {
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
}
}
return false;
}
@SuppressWarnings("deprecation")
public static boolean isMobileData(@NonNull Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
if (networkCapabilities != null) {
return networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
}
} else {
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected() && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
}
}
return false;
}
@SuppressWarnings("deprecation")
public static boolean isNetworkAvailable(@NonNull Context context) {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (manager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
NetworkCapabilities networkCapabilities = manager.getNetworkCapabilities(manager.getActiveNetwork());
if (networkCapabilities != null) {
return networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
} else {
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
}
return false;
}
}
有能力的小伙伴可以研究一下实现异步检测判断,欢迎大家评论交流
本文就到这里,喜欢的朋友能给一个免费的赞嘛,你的鼓励是我前进的动力!
|