android-验证网络是否可用
2012-05-27 09:05:51 来源:WEB开发网核心提示: State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState(); if(State.CONNECTED==state){ Log.i("TAG", &qu
State state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
if(State.CONNECTED==state){
Log.i("TAG", "GPRS网络已连接");
}
state = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
if(State.CONNECTED==state){
Log.i("TAG“, "WIFI网络已连接");
}
//判断网络是否可用 public static boolean isNetworkAvaiable(Context context){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo info = connectivityManager.getActiveNetworkInfo(); // 获取代表联网状态的NetWorkInfo对象 return (info != null && info.isConnected()); } //判断WIFI是否可用 public static boolean isWifiActive(Context context){ ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(CONNECTIVITY_SERVICE); NetworkInfo[] info = connectivityManager.getAllNetworkInfo(); if(info!=null){ for(int i=0;i<info.length;i++){ // if(info[i].getTypeName().equals(ConnectivityManager.TYPE_WIFI)&& info[i].isConnected()) return true; } } return false; }
ConnectivityManager.TYPE_WIFI 表示wifi连接
ConnectivityManager.TYPE_MOBILE 表示3G连接
更多精彩
赞助商链接