ConnectivityManager验证互联网连接

Mar*_*zzi 1 android package

大家好我需要验证我的设备当前是否已连接到互联网,所以我写了这个使用a 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

cit*_*onn 8

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;
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)