getAllNetworkInfo()是德precated

getAllNetworkInfo()是德precated

问题描述:

我写这篇code。

ConnectivityManager connectivity = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE); 

    if (connectivity != null) {
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info != null) {
            for (int i = 0; i < info.length; i++) {
                if (info[i].getState() == NetworkInfo.State.CONNECTED) {}

但getAllNetworkInfo()是德precated。请帮忙。谢谢你。

But getAllNetworkInfo() is deprecated. Please help. Thank you.

作为文档说:

getAllNetworkInfo()

这个方法在API级别23.此方法不支持相同类型的多个连接的网络pcated德$ P $。使用 getAllNetworks() getNetworkInfo(android.net.Network)代替。

This method was deprecated in API level 23. This method does not support multiple connected networks of the same type. Use getAllNetworks() and getNetworkInfo(android.net.Network) instead.

我认为你可以使用 getActiveNetworkInfo()

下面是code:

    ConnectivityManager connectivity = (ConnectivityManager)
            MainActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivity != null) {
        NetworkInfo nInfo=connectivity.getActiveNetworkInfo();

        if (nInfo != null && nInfo.getState()== NetworkInfo.State.CONNECTED) {
            //do your thing
        }
    }