Android 网络判断类,用来判断网络状态
使用方法:
(1)先初始化
//初始化网络状态检测类
NetworkStateManager.instance().init(this);
(2)判断是否联网
NetworkStateManager.instance().isNetworkConnected();
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class NetworkStateManager
{
static NetworkStateManager s_m = null;
private Context context;
private NetworkStateManager()
{
}
public void init(Context ctx)
{
context = ctx;
}
public static synchronized NetworkStateManager instance()
{
if (s_m == null)
{
s_m = new NetworkStateManager();
}
return s_m;
}
/**
* 判断是否有网络连接
* @return
*/
public boolean isNetworkConnected()
{
if (context == null)
{
return false;
}
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null)
{
return false;
} else
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
{
for (int i = 0; i < info.length; i++)
{
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
/**
* 判断WIFI网络是否可用
* @return
*/
public boolean isWifiConnected()
{
if (context != null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null)
{
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
/**
* 判断MOBILE网络是否可用
* @return
*/
public boolean isMobileConnected()
{
if (context != null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo != null)
{
return mMobileNetworkInfo.isAvailable();
}
}
return false;
}
public int getConnectedType()
{
if (context != null)
{
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager
.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable())
{
return mNetworkInfo.getType();
}
}
return -1;
}
}