网络连接判断的工具类,判断是wifi网络,还是蜂窝网络,或者无线网络;

public class Utils {


    /**
     * 返回值 -1:没有网络  1:WIFI网络   2:net网络
     */
    public static int getNetype(Context context) {
        int netType = -1;
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();


        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            netType = 2;
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 1;
        }
        return netType;
    }
}

猜你喜欢

转载自blog.csdn.net/lishaojie_521/article/details/79365355