繁体   English   中英

检查网络连接 android

[英]Check network connection android

在我在模拟器上测试的应用程序中,我使用以下代码检查网络连接(WIFI):

    public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

此方法始终返回true ,即使我禁用了计算机的无线连接......这是由模拟器引起的还是其他原因?

如果这不是检查网络连接的正确方法,我该怎么做?

最好 (1) 传入上下文,以便每个活动都可以调用此函数,以及 (2) 将此设为 function static:

 public boolean isNetworkOnline() {
    boolean status=false;
    try{
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getNetworkInfo(0);
        if (netInfo != null && netInfo.getState()==NetworkInfo.State.CONNECTED) {
            status= true;
        }else {
            netInfo = cm.getNetworkInfo(1);
            if(netInfo!=null && netInfo.getState()==NetworkInfo.State.CONNECTED)
                status= true;
        }
    }catch(Exception e){
        e.printStackTrace();  
        return false;
    }
    return status;

    }  

要使 getActiveNetworkInfo() 正常工作,您需要将以下内容添加到清单中。

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

最好使用 .netInfo.isConnected() 而不是 .netInfo.isConnectedOrConnecting

也试试这个

Context.getSystemService(Context.CONNECTIVITY_SERVICE).getNetworkInfo(ConnectivityManager.TYPE_MOBILE)

要么

Context.getSystemService(Context.CONNECTIVITY_SERVICE).requestRouteToHost(TYPE_WIFI, int hostAddress)

We can check inte.net connection connected state using below function. Google Android has updated the Android API's function to check the.network state. Now in Android latest API's, getAllNetworkInfo() is deprecated. 但是getAllNetworks() function 只允许Build.VERSION.SDK_INT >= 21.I已经为所有情况编写了代码。

要检查 inte.net 连接,您可以拨打此电话 function。

public static boolean checkInternetConnection(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
        return false;
    } else if(Build.VERSION.SDK_INT >= 21){
        Network[] info = connectivity.getAllNetworks();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i] != null && connectivity.getNetworkInfo(info[i]).isConnected()) {
                    return true;
                }
            }
        }
    } else {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
        final NetworkInfo activeNetwork = connectivity.getActiveNetworkInfo();
        if (activeNetwork != null && activeNetwork.isConnected()) {
            return true;
        }
    }

    return false;
}

就我而言,我关闭了 Wifi,但忘记关闭 Data/Mobile.network 连接。 一旦我这样做了,这个线程的原始帖子中的代码就对我有用了。

回顾一下:

  1. Go 到设置
  2. 关闭 Wifi
  3. Go 到设置->数据使用
  4. 关闭移动数据

设置

设置->数据使用

使用以下 class ,更新到最新的 Android 可用版本:Android 10。

这就是我将它放在一个项目中的方式:

清单文件:

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

Static Function:

public static boolean isNwConnected(Context context) {
    if (context == null) {
        return true;
    }
    ConnectivityManager connectivityManager = 
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo nwInfo = connectivityManager.getActiveNetworkInfo();
    if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM