当前位置: 首页>>代码示例>>Java>>正文


Java ConnectivityManager.getAllNetworks方法代码示例

本文整理汇总了Java中android.net.ConnectivityManager.getAllNetworks方法的典型用法代码示例。如果您正苦于以下问题:Java ConnectivityManager.getAllNetworks方法的具体用法?Java ConnectivityManager.getAllNetworks怎么用?Java ConnectivityManager.getAllNetworks使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.net.ConnectivityManager的用法示例。


在下文中一共展示了ConnectivityManager.getAllNetworks方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getConnectedNetworks

import android.net.ConnectivityManager; //导入方法依赖的package包/类
private static List<NetworkInfo> getConnectedNetworks(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return null;
    }
    final List<NetworkInfo> list = new ArrayList<NetworkInfo>();
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        NetworkInfo[] networkInfoList = cm.getAllNetworkInfo();
        if (networkInfoList != null) {
            final int length = networkInfoList.length;
            for (int i = 0; i < length; i++) {
                if (networkInfoList[i].getState() == NetworkInfo.State.CONNECTED) {
                    list.add(networkInfoList[i]);
                }
            }
        }
    } else {
        final Network[] networks = cm.getAllNetworks();
        if (networks != null && networks.length > 0) {
            NetworkInfo info;
            for (Network network : networks) {
                info = cm.getNetworkInfo(network);
                if (info != null && info.getState() == NetworkInfo.State.CONNECTED) {
                    list.add(info);
                }
            }
        }
    }
    return list;
}
 
开发者ID:LightSun,项目名称:android-util2,代码行数:31,代码来源:NetworkCompatUtil.java

示例2: findVpnNetwork

import android.net.ConnectivityManager; //导入方法依赖的package包/类
private Network findVpnNetwork() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    Network[] networks = cm.getAllNetworks();
    for (Network network : networks) {
        LinkProperties linkProperties = cm.getLinkProperties(network);
        List<LinkAddress> addresses = linkProperties.getLinkAddresses();
        for (LinkAddress addr : addresses) {
            if (addr.getAddress().equals(VPN_ADDRESS)) {
                return network;
            }
        }
    }
    return null;
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:15,代码来源:GnirehtetService.java

示例3: checkConnection

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@Contract("null -> false")
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static boolean checkConnection(@Nullable Context c) {
    if(c == null)
        return false;
    ConnectivityManager manager = (ConnectivityManager) c
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info;
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Network[] nets = manager.getAllNetworks();
        for (Network net : nets) {
            info = manager.getNetworkInfo(net);
            if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
                return true;
        }
        return false;
    } else {
        info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
            return true;
        info = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (info != null && info.getState() == NetworkInfo.State.CONNECTED)
            return true;
        info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        return info != null && info.getState() == NetworkInfo.State.CONNECTED;
    }
}
 
开发者ID:customerly,项目名称:Customerly-Android-SDK,代码行数:29,代码来源:IU_Utils.java

示例4: getLP

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private LinkProperties getLP(ConnectivityManager connMgr, int cap) {
    Network nets[] = connMgr.getAllNetworks();
    for (Network n: nets) {
        LinkProperties lp = connMgr.getLinkProperties(n);
        NetworkCapabilities np = connMgr.getNetworkCapabilities(n);
        String iname =  lp.getInterfaceName();
        if (iname != null && np != null) {
            //Log.d(TAG, ">>> " + iname + ": " + np.hasTransport(cap));
            if (np.hasTransport(cap))
                return lp;
        }
    }
    return null;
}
 
开发者ID:archos-sa,项目名称:aos-FileCoreLibrary,代码行数:16,代码来源:SambaDiscovery.java

示例5: onReceive

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)){
    //获得ConnectivityManager对象
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
        NetworkInfo wifiNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo dataNetworkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (wifiNetworkInfo.isConnected() || dataNetworkInfo.isConnected()) {
            mView.onRestoreNetworkRefreshUI();
        } else {
            mView.onDisconnectionRefreshUI();
        }
    }else {
        Network[] networks = connMgr.getAllNetworks();
        StringBuilder sb = new StringBuilder();
        //通过循环将网络信息逐个取出来
        for (int i=0; i < networks.length; i++){
            //获取ConnectivityManager对象对应的NetworkInfo对象
            NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
            if (networkInfo.isConnected()){
                mView.onRestoreNetworkRefreshUI();
                break;
            }else {
                mView.onDisconnectionRefreshUI();
                break;
            }
        }
    }
}
}
 
开发者ID:funnyzhaov,项目名称:Tribe,代码行数:32,代码来源:NetWorkStateReceiver.java

示例6: isConnectingToInternet

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * Checking for all possible internet providers
 * *
 */
public static boolean isConnectingToInternet(Context mContext) {
    ConnectivityManager connectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Network[] networks = connectivityManager.getAllNetworks();
        NetworkInfo networkInfo;
        for (Network mNetwork : networks) {
            networkInfo = connectivityManager.getNetworkInfo(mNetwork);
            if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                return true;
            }
        }
    } else {
        if (connectivityManager != null) {
            //noinspection deprecation
            NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
            if (info != null) {
                for (NetworkInfo anInfo : info) {
                    if (anInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
 
开发者ID:Dnet3,项目名称:CustomAndroidOneSheeld,代码行数:31,代码来源:ConnectionDetector.java

示例7: isConnected

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isConnected(@NonNull ConnectivityManager connMgr, int type) {
    Network[] networks = connMgr.getAllNetworks();
    NetworkInfo networkInfo;
    for (Network mNetwork : networks) {
        networkInfo = connMgr.getNetworkInfo(mNetwork);
        if (networkInfo != null && networkInfo.getType() == type && networkInfo.isConnected()) {
            return true;
        }
    }
    return false;
}
 
开发者ID:weimin96,项目名称:shareNote,代码行数:13,代码来源:NetworkUtils.java

示例8: lollipopChangeDefaultNetwork

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private void lollipopChangeDefaultNetwork(final ConnectivityManager cm, Context context) {
	Network[] array = cm.getAllNetworks();
	for (Network network : array) {
		NetworkInfo info = cm.getNetworkInfo(network);
		mLogger.info("Network: \"" + network + "\"\nInfo: " + info);
		
		if (info.getType() == ConnectivityManager.TYPE_WIFI) {
			mLogger.info("Setting the network: \"" + network + "\" as default (" + ConnectivityManager.setProcessDefaultNetwork(network) + ")");
		}
	}
}
 
开发者ID:hoanglm4,项目名称:RxAndroidTBP,代码行数:13,代码来源:WifiNetworkUtils.java

示例9: amIOnWiFi

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private boolean amIOnWiFi()
{
    ConnectivityManager connectivityManager = ((ConnectivityManager)activity.getSystemService
            (Context.CONNECTIVITY_SERVICE));
    boolean isWifiConnected = false;
    Network[] networks = connectivityManager.getAllNetworks();

    if (networks == null)
        isWifiConnected = false;
    else
    {
        for (Network network : networks)
        {
            NetworkInfo info = connectivityManager.getNetworkInfo(network);
            if (info != null && info.getType() == ConnectivityManager.TYPE_WIFI)
            {
                if (info.isAvailable() && info.isConnected())
                {
                    isWifiConnected = true;
                    break;
                }
            }
        }
    }
    return isWifiConnected;
}
 
开发者ID:MihaiBojescu,项目名称:Linux-notifier-Android,代码行数:28,代码来源:NetworkTools.java

示例10: isNetworkAvailable

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public boolean isNetworkAvailable() {
    boolean connected = false;
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    for (Network network : cm.getAllNetworks()) {
        NetworkInfo networkInfo = cm.getNetworkInfo(network);
        if (networkInfo.isConnected() == true) {
            connected = true;
            break;
        }
    }
    return connected;
}
 
开发者ID:PacktPublishing,项目名称:Java-9-Programming-Blueprints,代码行数:13,代码来源:MainActivity.java

示例11: onReceive

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    KLog.d("网络状态发生变化");
    //需要检测android版本是否小于21,因为getNetworkInfo(int networkType)被弃用
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        //获取ConnectivityManager对象
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //获取NetWorkInfo对象中WiFi连接信息
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        //获取NetWorkInfo对象中移动数据连接信息
        NetworkInfo dataNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (!wifiNetworkInfo.isConnected() && !dataNetworkInfo.isConnected()) {
            mNetWorkOnClick.netNotConnect();
        } else {
            if (!isNetworkOnline()) {
                mNetWorkOnClick.netNotOnLine();

            }
        }
    } else {
        KLog.d("API level 大于21");
        //获得ConnectivityManager对象
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        //获取所有网络连接的信息
        Network[] networks = connMgr.getAllNetworks();
        //通过循环将网络信息逐个取出来
        for (int i = 0; i < networks.length; i++) {
            //获取ConnectivityManager对象对应的NetworkInfo对象
            NetworkInfo networkInfo = connMgr.getNetworkInfo(networks[i]);
            if (networkInfo != null) {
                if (!networkInfo.isConnected()) {
                    mNetWorkOnClick.netNotConnect();
                } else {
                    if (!isNetworkOnline()) {
                        mNetWorkOnClick.netNotOnLine();
                    }
                }
            }
        }
    }
}
 
开发者ID:haihaio,项目名称:AmenEye,代码行数:46,代码来源:NetWorkStateReceiver.java

示例12: checkNetWorkState

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * 检测网络类型
 * @param context
 * @return
 */
public static int checkNetWorkState(@NonNull Context context) {

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

    //检测API是否小于21,因为API21之后getNetworkInfo(int networkType)方法被弃用
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

        //获取WIFI连接的信息
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        //获取移动数据连接的信息
        NetworkInfo mobileNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        if (wifiNetworkInfo.isConnected()) {
            return NETWORK_WIFI;
        } else if (mobileNetworkInfo.isConnected()) {
            return NETWORK_MOBILE;
        } else {
            return NETWORK_NONE;
        }

        /*NetworkInfo mNetworkInfo = connectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null && mNetworkInfo.isConnected()) {
            if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                return NETWORK_WIFI;
            } else if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                return NETWORK_MOBILE;
            } else {
                return NETWORK_NONE;
            }
        }*/

    } else {

        Network[] networks = connectivityManager.getAllNetworks();
        NetworkInfo mNetworkInfo;

        for (Network mNetwork : networks) {
            mNetworkInfo = connectivityManager.getNetworkInfo(mNetwork);
            if (mNetworkInfo != null) {
                if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                    //Log.e("------------>", "NETWORK_WIFI");
                    return NETWORK_WIFI;
                } else if (mNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                    //Log.e("------------>", "NETWORK_MOBILE");
                    return NETWORK_MOBILE;
                }
            } else {
                //Log.e("------------>", "NETWORK_NONE");
                return NETWORK_NONE;
            }
        }
    }
    return NETWORK_NONE;
}
 
开发者ID:weimin96,项目名称:shareNote,代码行数:60,代码来源:NetworkUtils.java


注:本文中的android.net.ConnectivityManager.getAllNetworks方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。