本文整理汇总了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;
}
示例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);
}
示例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;
}