在一个他人项目中看见一个写的比较好的网络判断代码
package com.immomo.momo.android.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
public class NetWorkUtils {
private Context mContext;
public State wifiState = null;
public State mobileState = null;
public NetWorkUtils(Context context) {
mContext = context;
}
public enum NetWorkState {
WIFI, MOBILE, NONE;
}
/**
* 获取当前的网络状态
*
* @return
*/
public NetWorkState getConnectState() {
ConnectivityManager manager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
manager.getActiveNetworkInfo();
wifiState = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
.getState();
mobileState = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
.getState();
if (wifiState != null && mobileState != null
&& State.CONNECTED != wifiState
&& State.CONNECTED == mobileState) {
return NetWorkState.MOBILE;
} else if (wifiState != null && mobileState != null
&& State.CONNECTED != wifiState
&& State.CONNECTED != mobileState) {
return NetWorkState.NONE;
} else if (wifiState != null && State.CONNECTED == wifiState) {
return NetWorkState.WIFI;
}
return NetWorkState.NONE;
}
}
其他类中很方便的判断是否存在网络或者所处的网络类型if (mNetWorkUtils.getConnectState() != NetWorkState.NONE)