ConnectivityManager 用于验证互联网连接

发布于 2024-11-29 11:18:39 字数 584 浏览 4 评论 0原文

大家好,我需要验证我的设备当前是否连接到互联网,因此我编写了这个使用 ConnectivityManager 来检查的类:

public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
        return true;

    } else {
        return false;
    }
}

效果很好,因为现在该方法位于main 包(com.App),但是我应该如何更改代码以使其在 com.App.Utility 中定义的类中工作?

谢谢!

Hi all I need to verify if my device is currently connected to internet or not and so I wrote this class that uses a ConnectivityManager to check:

public boolean checkInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
        return true;

    } else {
        return false;
    }
}

works great, because right now the method is in a class in the main package (com.App), but how should I change the code to make it work in a class defined in com.App.Utility ?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彼岸花ソ最美的依靠 2024-12-06 11:18:39
package com.app.utility;

  public class Utilities {

    public static final boolean CheckInternetConnection(Context context) {
      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
        return true;

      } else {
        return false;
      }
    }
  }
package com.app.utility;

  public class Utilities {

    public static final boolean CheckInternetConnection(Context context) {
      ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

      if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
        return true;

      } else {
        return false;
      }
    }
  }
瞄了个咪的 2024-12-06 11:18:39

使用 getActiveNetworkInfo() 的唯一问题是,在许多情况下它无法正确检测连接。例如,如果手机上的移动网络被禁用,但 wifi 可用,则活动网络仍可能被算作移动网络,因此返回 false。

另一种选择是:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfoMob = cm.getNetworkInfo(cm.TYPE_MOBILE);
    NetworkInfo netInfoWifi = cm.getNetworkInfo(cm.TYPE_WIFI);
    if ((netInfoMob != null || netInfoWifi != null) && (netInfoMob.isConnectedOrConnecting() || netInfoWifi.isConnectedOrConnecting())) {
        return true;
    }
    return false;
}

The only issue with using getActiveNetworkInfo() is that there are a number of situations where is will not correctly detect connectivity. For example if the mobile network is disabled on a phone but wifi is available then the active network may still be counted as the mobile network and therefore return false.

An alternative is:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfoMob = cm.getNetworkInfo(cm.TYPE_MOBILE);
    NetworkInfo netInfoWifi = cm.getNetworkInfo(cm.TYPE_WIFI);
    if ((netInfoMob != null || netInfoWifi != null) && (netInfoMob.isConnectedOrConnecting() || netInfoWifi.isConnectedOrConnecting())) {
        return true;
    }
    return false;
}
离鸿 2024-12-06 11:18:39

谢谢艾伦 H!

当netInfoMob和netInfoWifi之一为空而另一个不为空时,if条件可能会导致NPE。

这对我有用:

netInfoMobile != null && netInfoMobile.isConnectedOrConnecting() ||
netInfoWifi != null && netInfoWifi.isConnectedOrConnecting()

Thx Alan H!

The if condition may cause NPE when one of netInfoMob and netInfoWifi is null but the other not.

This one works for me:

netInfoMobile != null && netInfoMobile.isConnectedOrConnecting() ||
netInfoWifi != null && netInfoWifi.isConnectedOrConnecting()
肩上的翅膀 2024-12-06 11:18:39

试试这个,这段代码有效。

public static boolean checkNetwork(Context context) {
   context = context;
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if ((connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null && connManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected())
            || (connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && connManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnected())) {
        return true;
    } else {
        return false;
    }
}

Try this , this code works.

public static boolean checkNetwork(Context context) {
   context = context;
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    if ((connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null && connManager
            .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected())
            || (connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && connManager
            .getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnected())) {
        return true;
    } else {
        return false;
    }
}
梅倚清风 2024-12-06 11:18:39

您可以使用 kotlin 轻松检查。

 fun checkInternetConnection(context Context) {
     connectivityManager : ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?

     if(connectivityManager.activeNetwork) {
         return true
     } else {
         return false
     }
 } 

You can easily check with kotlin.

 fun checkInternetConnection(context Context) {
     connectivityManager : ConnectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?

     if(connectivityManager.activeNetwork) {
         return true
     } else {
         return false
     }
 } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文