在Android中很多应用都要检测网络连接的状态,下面介绍下检测网络连接的一个方法:
/* 检测网络连接状态 */
public static boolean CheckNetWork() { boolean result; ConnectivityManager cm = (ConnectivityManager) MyApplication .getInstance().getSystemService("connectivity"); NetworkInfo netinfo = cm.getActiveNetworkInfo(); if (netinfo != null && netinfo.isConnected()) { result = true; Log.i(TAG, "The net was connected"); } else { result = false; Log.i(TAG, "The net was bad!"); } return result; }