当前位置:   article > 正文

Android 网络状态判断_net_capability_validated

net_capability_validated

1、获取网络信息,首先需要获取权限

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

2.1我们通过ConnectivityManager可以获取状态,但是我们需要考虑到Android版本不同获取方式不同。

2.1.1hasCapability可以判断网络是否连接,常用参数如下:

 NetworkCapabilities.NET_CAPABILITY_INTERNET:判断是否连上网

NetworkCapabilities.NET_CAPABILITY_VALIDATED:判断当前是否能上网,但是发现小米11Android12添加证书后,一直是false
 

  1. private static boolean netWorkState(Context context) {
  2. if (context != null) {
  3. try {
  4. ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  5. // 通过版本去判断
  6. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  7. Network network = connectivityManager.getActiveNetwork();
  8. //network 为空,则表示没有网络
  9. if (network != null) {
  10. NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
  11. // NetworkCapabilities 为空,则表示没有网络
  12. if (capabilities != null) {
  13. //capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED) 小米11,android 12系统这个值一直是false
  14. return capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
  15. }
  16. }
  17. } else {
  18. NetworkInfo info = connectivityManager.getActiveNetworkInfo();
  19. if (info != null) {
  20. return info.isAvailable();
  21. }
  22. }
  23. return false;
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. return true;
  29. }

2.1.2hasCapability可以判断网络类型,常用参数如下:

NetworkCapabilities.TRANSPORT_WIFI:表示接入的是WIFI网络
NetworkCapabilities.TRANSPORT_CELLULAR:表示接入的是数据网络

  1. public static void networkType(NetworkCapabilities networkCapabilities) {
  2. if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
  3. //当前在使用Mobile流量上网
  4. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
  5. //当前在使用WiFi上网
  6. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
  7. //当前使用蓝牙上网
  8. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
  9. //当前使用以太网上网
  10. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_VPN)) {
  11. 当前使用VPN上网
  12. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) {
  13. //表示此网络使用Wi-Fi感知传输
  14. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_LOWPAN)) {
  15. //表示此网络使用LoWPAN传输
  16. } else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_USB)) {
  17. //表示此网络使用USB传输
  18. } else {
  19. //未知
  20. }
  21. }

2.2异步通过registerDefaultNetworkCallback获取网络状态(华为手机,在使用过程,网络状态不准确)

  1. ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  2. if (connectivityManager != null) {
  3. //注册 NetworkCallBack
  4. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
  5. connectivityManager.registerDefaultNetworkCallback(networkCallBack);
  6. }
  7. }

2.2.1networkCallBack

  1. private ConnectivityManager.NetworkCallback networkCallBack = new ConnectivityManager.NetworkCallback() {
  2. private static final String TAG = "TAG";
  3. @Override
  4. public void onCapabilitiesChanged(@NonNull Network network, @NonNull NetworkCapabilities networkCapabilities) {
  5. super.onCapabilitiesChanged(network, networkCapabilities);
  6. if (NetworkUtils.netWorkState(getBaseContext())) {
  7. Log.i(TAG, "onCapabilitiesChanged ---> ====网络可正常上网===网络类型为: " + NetworkUtils.networkType(MainActivity.this));
  8. }
  9. }
  10. @Override
  11. public void onAvailable(@NonNull Network network) {
  12. super.onAvailable(network);
  13. Log.i(TAG, "==网络连接成功,通知可以使用的时候调用====onAvailable===");
  14. }
  15. @Override
  16. public void onUnavailable() {
  17. super.onUnavailable();
  18. Log.i(TAG, "==当网络连接超时或网络请求达不到可用要求时调用====onUnavailable===");
  19. }
  20. @Override
  21. public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {
  22. super.onBlockedStatusChanged(network, blocked);
  23. Log.i(TAG, "==当访问指定的网络被阻止或解除阻塞时调用===onBlockedStatusChanged==");
  24. }
  25. @Override
  26. public void onLosing(@NonNull Network network, int maxMsToLive) {
  27. super.onLosing(network, maxMsToLive);
  28. Log.i(TAG, "==当网络正在断开连接时调用===onLosing===");
  29. }
  30. @Override
  31. public void onLost(@NonNull Network network) {
  32. super.onLost(network);
  33. Log.i(TAG, "==当网络已断开连接时调用===onLost===");
  34. }
  35. @Override
  36. public void onLinkPropertiesChanged(@NonNull Network network, @NonNull LinkProperties linkProperties) {
  37. super.onLinkPropertiesChanged(network, linkProperties);
  38. Log.i(TAG, "==当网络连接的属性被修改时调用===onLinkPropertiesChanged===");
  39. }
  40. };

2.3异步通过ping方式判断网络状态

  1. try {
  2. Runtime runtime = Runtime.getRuntime();
  3. Process p = runtime.exec("ping -c 1 8.8.8.8");
  4. int waitFor = p.waitFor();
  5. return waitFor == 0;
  6. } catch (IOException | InterruptedException e) {
  7. e.printStackTrace();
  8. }
  9. return false;

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/295501
推荐阅读
相关标签
  

闽ICP备14008679号