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


Java ConnectivityManager.getActiveNetwork方法代码示例

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


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

示例1: getDnsServers

import android.net.ConnectivityManager; //导入方法依赖的package包/类
@TargetApi(23)
private static List<InetAddress> getDnsServers(Context context) {
    List<InetAddress> servers = new ArrayList<>();
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //Network[] networks = connectivityManager == null ? null : connectivityManager.getAllNetworks();
    Network[] networks = connectivityManager == null ? null : new Network[]{connectivityManager.getActiveNetwork()};
    if (networks == null) {
        return servers;
    }
    for(int i = 0; i < networks.length; ++i) {
        LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]);
        if (linkProperties != null) {
            servers.addAll(linkProperties.getDnsServers());
        }
    }

    for(InetAddress server : servers) {
        Log.d("dns","DNS server: " + Strings.nullToEmpty(server.getHostName()) + " (" + server.getHostAddress() + ")");
    }

    return servers;
}
 
开发者ID:rtr-nettest,项目名称:open-rmbt,代码行数:23,代码来源:RMBTTask.java

示例2: hasInternetConnection

import android.net.ConnectivityManager; //导入方法依赖的package包/类
/**
 * @param context the current application context
 * @return whether the device was connected to the internet
 */
public static boolean hasInternetConnection(Context context) {

    /*
    Taken from Johan's answer at: https://stackoverflow.com/a/35009615
     */

    ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
    Network network;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        network = connectivityManager.getActiveNetwork();
    } else
        return true;
    NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
    return capabilities != null && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
}
 
开发者ID:CMPUT301F17T15,项目名称:CIA,代码行数:20,代码来源:NetworkUtilities.java

示例3: getDefaultDNS

import android.net.ConnectivityManager; //导入方法依赖的package包/类
public static List<String> getDefaultDNS(Context context) {
    String dns1 = null;
    String dns2 = null;
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network an = cm.getActiveNetwork();
        if (an != null) {
            LinkProperties lp = cm.getLinkProperties(an);
            if (lp != null) {
                List<InetAddress> dns = lp.getDnsServers();
                if (dns != null) {
                    if (dns.size() > 0)
                        dns1 = dns.get(0).getHostAddress();
                    if (dns.size() > 1)
                        dns2 = dns.get(1).getHostAddress();
                    for (InetAddress d : dns)
                        Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                }
            }
        }
    } else {
        dns1 = jni_getprop("net.dns1");
        dns2 = jni_getprop("net.dns2");
    }

    List<String> listDns = new ArrayList<>();
    listDns.add(TextUtils.isEmpty(dns1) ? "8.8.8.8" : dns1);
    listDns.add(TextUtils.isEmpty(dns2) ? "8.8.4.4" : dns2);
    return listDns;
}
 
开发者ID:miankai,项目名称:MKAPP,代码行数:31,代码来源:Util.java


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