getAllNetworkInfo()已弃用

时间:2015-10-20 00:31:54

标签: android networking connection android-6.0-marshmallow

我写了这段代码。

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()。请帮忙。谢谢。

3 个答案:

答案 0 :(得分:5)

正如文档所说:

getAllNetworkInfo()

  

此方法在API级别23中已弃用。此方法不支持同一类型的多个连接网络。请改用getAllNetworks()getNetworkInfo(android.net.Network)

我认为你可以使用getActiveNetworkInfo()

以下是代码:

    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
        }
    }

答案 1 :(得分:0)

public class ConnectionDetector {

private Context _context;

public ConnectionDetector(Context context) {
    this._context = context;
}

public boolean isConnectingToInternet() {
    ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Network[] networks = connectivity.getAllNetworks();
        NetworkInfo networkInfo;
        for (Network mNetwork : networks) {
            networkInfo = connectivity.getNetworkInfo(mNetwork);
            if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                return true;
            }
        }
    } else {
        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) {
                        return true;
                    }
                }

        }
    }

    return false;
}}

您可以使用此类检测互联网连接。希望这会对你有所帮助。

答案 2 :(得分:0)

请参阅我的回答:https://stackoverflow.com/a/32771164/4279451

您可以使用:

  

getActiveNetworkInfo();

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                // connected to wifi
                Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                // connected to the mobile provider's data plan
                Toast.makeText(context, activeNetwork.getTypeName(), Toast.LENGTH_SHORT).show();
            }
        } else {
            // not connected to the internet
        }