推荐一个工具类吧 但是有部分的车机系统他就不支持绝大部分公共代码
在不是wifi的情况下 去获取以太网络信息 来最终判断是否是有网络
package com.tencent.wcenter.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import com.tencent.jsbridge.LogUtils;
import com.tencent.wcenter.constants.Constant;
public class NetWorkUtils {
/**
* * 判断是否有网络连接
* <p>
* * @param context
* <p>
* * @return
* <p>
*
*/
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;
}
/**
* * 判断WIFI网络是否可用
* <p>
* * @param context
* <p>
* * @return
* <p>
*
*/
public static boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}
//这里的这个方法就是判别车机自身带有4G网络的方法
//在不是wifi的情况下 去获取以太网络信息 来最终判断是否是有网络
public static boolean isNetworkAvailable(Context mContext) {
ConnectivityManager connectMgr = (ConnectivityManager) mContext.getSystemService(mContext.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
LogUtils.i("isNetworkAvailable", "networkInfo :" + networkInfo);
if (!networkInfo.isConnected()) {
networkInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
LogUtils.i("isNetworkAvailable", "networkInfo :" + networkInfo);
}
return networkInfo.isConnected();
}
/**
* * 判断MOBILE网络是否可用
* <p>
* * @param context
* <p>
* * @return
* <p>
*
*/
public static 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;
}
/**
* * 获取当前网络连接的类型信息
* <p>
* * @param context
* <p>
* * @return
* <p>
*
*/
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}
/**
* * 获取当前的网络状态 :没有网络0:WIFI网络1:3G网络2:2G网络3
* <p>
* *
* <p>
* * @param context
* <p>
* * @return
* <p>
*
*/
public static int getAPNType(Context context) {
int netType = 0;
ConnectivityManager connMgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
LogUtils.i("NetWorkUtils","nType="+netType);
if (nType == ConnectivityManager.TYPE_WIFI) {
netType = 1;// wifi
} else if (nType == ConnectivityManager.TYPE_MOBILE) {
int nSubType = networkInfo.getSubtype();
TelephonyManager mTelephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
if (nSubType == TelephonyManager.NETWORK_TYPE_UMTS
&& !mTelephony.isNetworkRoaming()) {
netType = 2;// 3G
} else if (nSubType == TelephonyManager.NETWORK_TYPE_LTE){
netType = 3;// 4G
}else {
netType = 4;// 2G
}
}
return netType;
}
}
网友评论