private ConnectivityManager mConnectivity;
private TelephonyManager mTelephony;
/** 检查网络状态 */
public boolean checkConnectionState() {
/* 检查有没有网络 */
NetworkInfo info = mConnectivity.getActiveNetworkInfo();
if (info == null || !mConnectivity.getBackgroundDataSetting()) {
return false;
}
/* 判断网络连接类型,只有在3G 或 wifi 里进行一些数据更新。 */
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI) {
return info.isConnected();
} else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS && !mTelephony.isNetworkRoaming()) {
return info.isConnected();
} else {
return false;
}
}
andorid 判断网络状态和网络类型
最新推荐文章于 2021-05-29 07:38:15 发布