验证Android Internet连接和错误

时间:2015-09-13 18:47:17

标签: java android android-service

在服务中我使用以下功能验证互联网连接:

 public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return !(networkInfo == null || !networkInfo.isConnected());
}

问题是如果我没有Internet连接,我会在Log中获得以下内容:

 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
 I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
 I/System.out: KnoxVpnUidStorageknoxVpnSupported API value returned is false

所以Android 5.1.1告诉我,我的应用程序正在减慢我的设备速度,并且始终存在错误。

问题是什么?感谢。

1 个答案:

答案 0 :(得分:5)

Tis is the function that implements to check the Internet connection.

public boolean isOnline(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo networkinfo = cm.getActiveNetworkInfo();
    if (networkinfo != null && networkinfo.isConnected()) {
        return true;
    }
    return false;
}