android 网络连接的判断

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



package com.wzq.wifidemo;



import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;


/**
 * author:Created by WangZhiQiang on 2017-09-05.
 */


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/hubianyu/article/details/78413323