本文整理汇总了Java中android.net.Network类的典型用法代码示例。如果您正苦于以下问题:Java Network类的具体用法?Java Network怎么用?Java Network使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Network类属于android.net包,在下文中一共展示了Network类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getConnectedNetworks
import android.net.Network; //导入依赖的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;
}
示例2: testConnectivityManagerDelegateDoesNotCrash
import android.net.Network; //导入依赖的package包/类
@UiThreadTest
@MediumTest
@Feature({"Android-AppBase"})
public void testConnectivityManagerDelegateDoesNotCrash() {
ConnectivityManagerDelegate delegate =
new ConnectivityManagerDelegate(getInstrumentation().getTargetContext());
delegate.getNetworkState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// getNetworkState(Network) doesn't crash upon invalid Network argument.
Network invalidNetwork = netIdToNetwork(NetId.INVALID);
NetworkState invalidNetworkState = delegate.getNetworkState(invalidNetwork);
assertFalse(invalidNetworkState.isConnected());
assertEquals(-1, invalidNetworkState.getNetworkType());
assertEquals(-1, invalidNetworkState.getNetworkSubType());
Network[] networks = delegate.getAllNetworksUnfiltered();
if (networks.length >= 1) {
delegate.getNetworkState(networks[0]);
}
delegate.getDefaultNetId();
NetworkCallback networkCallback = new NetworkCallback();
NetworkRequest networkRequest = new NetworkRequest.Builder().build();
delegate.registerNetworkCallback(networkRequest, networkCallback);
delegate.unregisterNetworkCallback(networkCallback);
}
}
示例3: getDnsServers
import android.net.Network; //导入依赖的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;
}
示例4: hasInternetConnection
import android.net.Network; //导入依赖的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);
}
示例5: onCapabilitiesChanged
import android.net.Network; //导入依赖的package包/类
@Override
public void onCapabilitiesChanged(
Network network, NetworkCapabilities networkCapabilities) {
if (ignoreConnectedNetwork(network, networkCapabilities)) {
return;
}
// A capabilities change may indicate the ConnectionType has changed,
// so forward the new ConnectionType along to observer.
final long netId = networkToNetId(network);
final int connectionType =
getCurrentConnectionType(mConnectivityManagerDelegate.getNetworkState(network));
ThreadUtils.postOnUiThread(new Runnable() {
@Override
public void run() {
mObserver.onNetworkConnect(netId, connectionType);
}
});
}
示例6: findVpnNetwork
import android.net.Network; //导入依赖的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;
}
示例7: testConnectivityManagerDelegateDoesNotCrash
import android.net.Network; //导入依赖的package包/类
/**
* Tests that ConnectivityManagerDelegate doesn't crash. This test cannot rely on having any
* active network connections so it cannot usefully check results, but it can at least check
* that the functions don't crash.
*/
@Test
@UiThreadTest
@SmallTest
public void testConnectivityManagerDelegateDoesNotCrash() {
ConnectivityManagerDelegate delegate =
new ConnectivityManagerDelegate(InstrumentationRegistry.getTargetContext());
delegate.getNetworkState();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Network[] networks = delegate.getAllNetworks();
if (networks.length >= 1) {
delegate.getNetworkState(networks[0]);
delegate.hasInternetCapability(networks[0]);
}
delegate.getDefaultNetId();
}
}
示例8: getNetworkInfo
import android.net.Network; //导入依赖的package包/类
@RequiresApi(LOLLIPOP)
@Nullable
static NetworkInfo getNetworkInfo(@NonNull Network network,
@NonNull ConnectivityManager connectivityManager) {
NetworkInfo networkInfo = null;
try {
networkInfo = connectivityManager.getNetworkInfo(network);
} catch (Exception exc) {
logger.log(WARNING,
"Could not retrieve network info from provided network: " + exc.getMessage());
}
return networkInfo;
}
示例9: registerLollipopNetworkReceiver
import android.net.Network; //导入依赖的package包/类
public static void registerLollipopNetworkReceiver(Context context) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
cm.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.build(),
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
boolean connected = false;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
connected = cm.bindProcessToNetwork(network);
} else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
//noinspection deprecation
connected = ConnectivityManager.setProcessDefaultNetwork(network);
}
if(connected) {
Customerly.get().__SOCKET__check();
}
}
});
}
}
示例10: checkConnection
import android.net.Network; //导入依赖的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;
}
}
示例11: registerNetworkCallback
import android.net.Network; //导入依赖的package包/类
private void registerNetworkCallback(Context context) {
final ConnectivityManager manager =
(ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE);
manager.registerNetworkCallback(
new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
.build(),
new NetworkCallback() {
@Override
public void onAvailable(Network network) {
mSambaClient.reset();
}
});
}
示例12: networkCallback
import android.net.Network; //导入依赖的package包/类
@TargetApi(LOLLIPOP)
NetworkCallback networkCallback(final String networkSSID,
final NetworkStateChangeListener listener) {
return new NetworkCallback() {
@Override
public void onAvailable(Network network) {
NetworkInfo networkInfo = manager.getNetworkInfo(network);
logger.i("Network is Available. Network Info: " + networkInfo);
if (WifiHelper.areEqual(networkInfo.getExtraInfo(), networkSSID)) {
manager.unregisterNetworkCallback(this);
networkCallback = null;
bindToRequiredNetwork(network);
logger.i(format("Bound application to use %s network", networkSSID));
listener.onNetworkBound();
}
}
};
}
示例13: logNetworks
import android.net.Network; //导入依赖的package包/类
@SuppressWarnings("deprecation")
private static void logNetworks(ConnectivityManager connectivityManager) {
NetworkInfo[] networkInfos;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
networkInfos = connectivityManager.getAllNetworkInfo();
} else {
Network[] networks = connectivityManager.getAllNetworks();
networkInfos = new NetworkInfo[networks.length];
for (int i = 0; i < networks.length; i++) {
networkInfos[i] = connectivityManager.getNetworkInfo(networks[i]);
}
}
for (NetworkInfo networkInfo : networkInfos)
log(networkInfo);
}
示例14: getDnsServers
import android.net.Network; //导入依赖的package包/类
private static Set<InetAddress> getDnsServers(Context context) throws VpnNetworkException {
Set<InetAddress> out = new HashSet<>();
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(VpnService.CONNECTIVITY_SERVICE);
// Seriously, Android? Seriously?
NetworkInfo activeInfo = cm.getActiveNetworkInfo();
if (activeInfo == null)
throw new VpnNetworkException("No DNS Server");
for (Network nw : cm.getAllNetworks()) {
NetworkInfo ni = cm.getNetworkInfo(nw);
if (ni == null || !ni.isConnected() || ni.getType() != activeInfo.getType()
|| ni.getSubtype() != activeInfo.getSubtype())
continue;
for (InetAddress address : cm.getLinkProperties(nw).getDnsServers())
out.add(address);
}
return out;
}
示例15: getDnsServers
import android.net.Network; //导入依赖的package包/类
@TargetApi(21)
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();
if (networks == null) {
return getDnsServersPreLollipop();
}
for(int i = 0; i < networks.length; ++i) {
LinkProperties linkProperties = connectivityManager.getLinkProperties(networks[i]);
if (linkProperties != null) {
servers.addAll(linkProperties.getDnsServers());
}
}
if (servers.size() > 0) {
Log.d(Config.LOGTAG,"used lollipop variant to discover dns servers in "+networks.length+" networks");
}
return servers.size() > 0 ? servers : getDnsServersPreLollipop();
}