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


Java NetworkInfo.getState方法代码示例

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


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

示例1: isNetworkAvailable

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 判断网络是否可用
 */
public static boolean isNetworkAvailable(Context context) {

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null != connectivity) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:lo625090140,项目名称:lqrwechatrongcloud,代码行数:19,代码来源:NetUtils.java

示例2: getConnectedNetworks

import android.net.NetworkInfo; //导入方法依赖的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

示例3: isNetworkAvailable

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 检查当前网络是否可用
 *
 * @param activity 活动
 * @return boolean boolean
 */
public static boolean isNetworkAvailable(@NonNull Activity activity) {
    Context context = activity.getApplicationContext();
    // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (connectivityManager == null) {
        return false;
    } else {
        // 获取NetworkInfo对象
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
        if (networkInfo != null && networkInfo.length > 0) {
            for (NetworkInfo aNetworkInfo : networkInfo) {
                // 判断当前网络状态是否为连接状态
                if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
开发者ID:yangchaojiang,项目名称:yjPlay,代码行数:28,代码来源:VideoPlayUtils.java

示例4: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getExtras() != null) {
        NetworkInfo ni = (NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (ni != null && ni.getState() == NetworkInfo.State.CONNECTED) {
            Log.i("app", "Network " + ni.getTypeName() + " connected");
            if (connectionCallback != null) {
                connectionCallback.hasActiveConnection(true);
            }
        }
    }
    if (intent.getExtras().getBoolean(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false)) {
        Log.d("app", "There's no network connectivity");
        if (connectionCallback != null) {
            connectionCallback.hasActiveConnection(false);
        }
    }
}
 
开发者ID:appwise-labs,项目名称:NoInternetDialog,代码行数:19,代码来源:NetworkStatusReceiver.java

示例5: ConnectivityReceiver

import android.net.NetworkInfo; //导入方法依赖的package包/类
public ConnectivityReceiver(TerminalManager manager, boolean lockingWifi) {
	mTerminalManager = manager;

	final ConnectivityManager cm =
			(ConnectivityManager) manager.getSystemService(Context.CONNECTIVITY_SERVICE);

	final WifiManager wm = (WifiManager) manager.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
	mWifiLock = wm.createWifiLock(TAG);

	final NetworkInfo info = cm.getActiveNetworkInfo();
	if (info != null) {
		mIsConnected = (info.getState() == State.CONNECTED);
	}

	mLockingWifi = lockingWifi;

	final IntentFilter filter = new IntentFilter();
	filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
	manager.registerReceiver(this, filter);
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:21,代码来源:ConnectivityReceiver.java

示例6: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
	final String action = intent.getAction();

	if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
		Log.w(TAG, "onReceived() called: " + intent);
		return;
	}

	boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
	boolean isFailover = intent.getBooleanExtra(ConnectivityManager.EXTRA_IS_FAILOVER, false);

	Log.d(TAG, "onReceived() called; noConnectivity? " + noConnectivity + "; isFailover? " + isFailover);

	if (noConnectivity && !isFailover && mIsConnected) {
		mIsConnected = false;
		mTerminalManager.onConnectivityLost();
	} else if (!mIsConnected) {
		NetworkInfo info = (NetworkInfo) intent.getExtras()
				.get(ConnectivityManager.EXTRA_NETWORK_INFO);

		if (mIsConnected = (info.getState() == State.CONNECTED)) {
			mTerminalManager.onConnectivityRestored();
		}
	}
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:27,代码来源:ConnectivityReceiver.java

示例7: checkNet

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * @param context c
 * @return true网路可用。false不可用
 * @since 1.0.0
 */
public static boolean checkNet(Context context) {
    try {
        //获取连接管理对象
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            //获取活动的网络连接
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        LogUtils.e(e);
    }
    return false;
}
 
开发者ID:jessie345,项目名称:RetrofitAppArchitecture,代码行数:25,代码来源:Utils.java

示例8: isConnected

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 判断网络是否连接
 */
public static boolean isConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);

    if (null == cm) {
        return false;
    }

    NetworkInfo info = cm.getActiveNetworkInfo();
    if (null != info && info.isConnected()) {
        if (info.getState() == NetworkInfo.State.CONNECTED) {
            return true;
        }
    }
    return false;
}
 
开发者ID:Horrarndoo,项目名称:YiZhi,代码行数:20,代码来源:NetworkConnectionUtils.java

示例9: isNetworkAvailable

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 描述:判断网络是否有效.
 *
 * @return true, if is network available
 */
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}
 
开发者ID:guzhigang001,项目名称:Bailan,代码行数:23,代码来源:AppUtil.java

示例10: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            if (isConnected) {
                MySharedPre.getInstance().putIpAddress(Utils.getIpAddress());
            } else {
                MySharedPre.getInstance().putIpAddress("0.0.0.0");
            }
        }
    }
}
 
开发者ID:Zane96,项目名称:Fairy,代码行数:17,代码来源:WifiStateReceiver.java

示例11: isConnected

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 判断网络是否连接
 *
 * @param context context
 * @return true is connected
 */
@Deprecated
public static boolean isConnected(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (null != connectivity) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
开发者ID:shenhuanet,项目名称:AndroidOpen,代码行数:20,代码来源:NetworkUtils.java

示例12: checkNetwork

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * Prüft, ob das aktuelle Gerät mit dem Internet verbunden ist.
 *
 * @return true, falls eine aktive Netzwerkverbindung besteht; false, falls nicht
 */
public static boolean checkNetwork() {
    ConnectivityManager c = (ConnectivityManager) getController().getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    if (c != null) {
        NetworkInfo n = c.getActiveNetworkInfo();
        if (n != null) {
            return n.getState() == NetworkInfo.State.CONNECTED;
        }
    }
    return false;
}
 
开发者ID:LCA311,项目名称:leoapp-sources,代码行数:16,代码来源:Utils.java

示例13: isNetworkConnected

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static boolean isNetworkConnected(Context context) {
	ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
	if (networkInfo != null && networkInfo.isConnectedOrConnecting()) {
		if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
			return true;
		}
	}
	return false;
}
 
开发者ID:PlutoArchitecture,项目名称:Pluto-Android,代码行数:11,代码来源:NetworkUtils.java

示例14: k

import android.net.NetworkInfo; //导入方法依赖的package包/类
public static String[] k(Context context) {
    String[] strArr = new String[]{"", ""};
    try {
        if (a(context, "android.permission.ACCESS_NETWORK_STATE")) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
            if (connectivityManager == null) {
                strArr[0] = "";
                return strArr;
            } else if (connectivityManager.getNetworkInfo(1).getState() == State.CONNECTED) {
                strArr[0] = d;
                return strArr;
            } else {
                NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
                if (networkInfo.getState() == State.CONNECTED) {
                    strArr[0] = c;
                    strArr[1] = networkInfo.getSubtypeName();
                    return strArr;
                }
                return strArr;
            }
        }
        strArr[0] = "";
        return strArr;
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
开发者ID:JackChan1999,项目名称:boohee_v5.6,代码行数:28,代码来源:bt.java

示例15: getNetworkType

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取网络连接状态
 */
public static int getNetworkType(Context context) {
    // NetworkInfo 有以下方法
    // getDetailedState():获取详细状态。
    // getExtraInfo():获取附加信息(3gnet/3gwap/uninet/uniwap/cmnet/cmwap/ctnet/ctwap)
    // getReason():获取连接失败的原因。
    // getType():获取网络类型(一般为移动或Wi-Fi)。
    // getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
    // isAvailable():判断该网络是否可用。
    // isConnected():判断是否已经连接。
    // isConnectedOrConnecting():判断是否已经连接或正在连接。
    // isFailover():判断是否连接失败。
    // isRoaming():判断是否漫游

    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivity.getActiveNetworkInfo();
    if (networkInfo != null) {
        int type = networkInfo.getType();
        State state = networkInfo.getState();

        if (type == ConnectivityManager.TYPE_WIFI
                && state == State.CONNECTED) {
            // wifi连接通畅
            return NETWORK_CONNETED_WIFI;
        } else if (type == ConnectivityManager.TYPE_MOBILE
                && state == State.CONNECTED) {
            return NETWORK_CONNETED_GPRS;
        }
    }
    return NETWORK_DISCONNETED;
}
 
开发者ID:FacePlusPlus,项目名称:MegviiLicMgr-Android-SDK,代码行数:36,代码来源:NetWorkHelper.java


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