清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>
在用Android进行网络操作时,首先得判断网络是否连通,以下代码可以检测:
public static boolean isNetworkAvailable(Context context){ ConnectivityManager mgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo[] info =mgr.getAllNetworkInfo(); if (info != null) { for (int i = 0; i < info.length; i++) { if (info[i].getState() == NetworkInfo.State.CONNECTED){ return true; } } } return false; }