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


Java Network类代码示例

本文整理汇总了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;
}
 
开发者ID:LightSun,项目名称:android-util2,代码行数:31,代码来源:NetworkCompatUtil.java

示例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);
    }
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:27,代码来源:NetworkChangeNotifierTest.java

示例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;
}
 
开发者ID:rtr-nettest,项目名称:open-rmbt,代码行数:23,代码来源:RMBTTask.java

示例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);
}
 
开发者ID:CMPUT301F17T15,项目名称:CIA,代码行数:20,代码来源:NetworkUtilities.java

示例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);
        }
    });
}
 
开发者ID:lizhangqu,项目名称:chromium-net-for-android,代码行数:19,代码来源:NetworkChangeNotifierAutoDetect.java

示例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;
}
 
开发者ID:Genymobile,项目名称:gnirehtet,代码行数:15,代码来源:GnirehtetService.java

示例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();
  }
}
 
开发者ID:lgyjg,项目名称:AndroidRTC,代码行数:22,代码来源:NetworkMonitorTest.java

示例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;
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:17,代码来源:RxNetworkInfo.java

示例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();
                        }
                    }
                });
    }
}
 
开发者ID:customerly,项目名称:Customerly-Android-SDK,代码行数:26,代码来源:IU_NetworkReceiver.java

示例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;
    }
}
 
开发者ID:customerly,项目名称:Customerly-Android-SDK,代码行数:29,代码来源:IU_Utils.java

示例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();
        }
      });
}
 
开发者ID:google,项目名称:samba-documents-provider,代码行数:16,代码来源:SambaProviderApplication.java

示例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();
            }
        }
    };
}
 
开发者ID:nishkarsh,项目名称:android-wificonnect,代码行数:20,代码来源:WifiUtil.java

示例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);
}
 
开发者ID:Flowdalic,项目名称:android-xmpp-iot-demo,代码行数:17,代码来源:NetworkConnectivityReceiver.java

示例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;
}
 
开发者ID:julian-klode,项目名称:dns66,代码行数:19,代码来源:AdVpnThread.java

示例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();
}
 
开发者ID:xavierle,项目名称:messengerxmpp,代码行数:20,代码来源:DNSHelper.java


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