之前用到判断网络状态,有些情况下还需要判断是wifi网络,还是移动网络,所以,干脆就把这个工具类分享了,也用javadoc,工具生成了大致的文档。简单可以看下。
package net.nyist.netState;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**@author
*
*这是一个判断当前网络状态的工具类
*
*/
public class netState {
/**
*
* @return 是否有活动的网络连接
*/
public final boolean hasNetWorkConnection(Context context){
//获取连接活动管理器
final ConnectivityManager connectivityManager= (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
//获取链接网络信息
final NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
return (networkInfo!= null && networkInfo.isAvailable());
}
/**
* @return 返回boolean ,是否为wifi网络
*
*/
public final boolean hasWifiConnection(Context context)
{
final ConnectivityManager connectivityManager= (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//是否有网络并且已经连接
return (networkInfo!=null&& networkInfo.isConnectedOrConnecting());
}
/**
* @return 返回boolean,判断网络是否可用,是否为移动网络
*
*/
public final boolean hasGPRSConnection(Context context){
//获取活动连接管理器
final ConnectivityManager connectivityManager= (ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (networkInfo!=null && networkInfo.isAvailable());
}
/**
* @return 判断网络是否可用,并返回网络类型,ConnectivityManager.TYPE_WIFI,ConnectivityManager.TYPE_MOBILE,不可用返回-1
*/
public static final int getNetWorkConnectionType(Context context){
final ConnectivityManager connectivityManager=(ConnectivityManager) context.
getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo wifiNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final NetworkInfo mobileNetworkInfo=connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifiNetworkInfo!=null &&wifiNetworkInfo.isAvailable())
{
return ConnectivityManager.TYPE_WIFI;
}
else if(mobileNetworkInfo!=null &&mobileNetworkInfo.isAvailable())
{
return ConnectivityManager.TYPE_MOBILE;
}
else {
return -1;
}
}
}