许多app都会用到网络,但怎样来判断当前是否有网络连接。
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;}
如果我们手机打开着数据流量,那怎样来判断数据流量是否有网呢。
public boolean isMobileConnected(Context context) {if (context != null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);if (mMobileNetworkInfo != null) {return mMobileNetworkInfo.isAvailable();}}return false;}
好了,先就这了。