// 判断网络连接是否正常
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm == null) {
} else {
// 如果仅仅是用来判断网络连接
// 则可以使用 cm.getActiveNetworkInfo().isAvailable();
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
Toast.makeText(context, "网络连接正常", 0).show();
return true;
}
}
}
}
Toast.makeText(context, "网络连接失败", 0).show();
return false;
}
原文:http://www.cnblogs.com/cuizhe/p/5459589.html