判断是否有网络连接

不多比比直接上代码!!!

public static boolean is_Network_Available(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE );
if (cm == null) {
} else {
NetworkInfo[] info = cm.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
}
return false;
}

【判断是否有网络连接】

下面这个直接在MainActivity的数据请求上面写就好使

if (NetWorkUtil.is_Network_Available( this ) || NetWorkUtil.is_Network_Available( this )) {
presenter = new Presenter( this );
presenter.getPost( Api.LEFT_URL, page );
} else {
Toast.makeText( this, "网络不通畅,请稍后再试!", Toast.LENGTH_SHORT ).show();
}

    推荐阅读