// 判断网络状态 public 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 (isNetworkConnected(this)) { Toast.makeText(getApplicationContext(), "有网", 1000).show(); } else { Toast.makeText(getApplicationContext(), "没网", 1000).show(); }