public class NetTools {
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}
}
用法:
if (!NetTools.isNetworkConnected(this)) {
// 提示当前网络不可用
ToastUtil.showMessage(this, getString(R.string.common_net_error));
return;
}