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