NetWorkUtil网络状态

移动开发 waitig 403℃ 百度已收录 0评论

1.首先呢,我们设置网络状态的常量

public class NetWorkStatusConstants {
    /**
     * Unknown network class
     */
    public static final int NETWORK_CLASS_UNKNOWN = 0;
    /**
     * wifi networks
     */
    public static final int NETWORK_WIFI          = 1;
    /**
     * "2G" networks
     */
    public static final int NETWORK_CLASS_2_G     = 2;
    /**
     * "3G" networks
     */
    public static final int NETWORK_CLASS_3_G     = 3;
    /**
     * "4G" networks
     */
    public static final int NETWORK_CLASS_4_G     = 4;
}

NetWorkUtil

public class NetWorkUtil {

    /**
     * 获得网络连接是否可用
     * @param context
     * @return
     */

    public static boolean hasNetwork(Context context) {
        ConnectivityManager con = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo workinfo = con.getActiveNetworkInfo();
        if (workinfo == null || !workinfo.isAvailable()) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否是wifi连接
     */

    public static boolean checkWifiState(Context context) {
        boolean isWifiConnect = true;
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
        for (int i = 0; i < networkInfos.length; i++) {
            if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
                if (networkInfos[i].getType() == cm.TYPE_MOBILE) {
                    isWifiConnect = false;
                }
                if (networkInfos[i].getType() == cm.TYPE_WIFI) {
                    isWifiConnect = true;
                }
            }
        }
        return isWifiConnect;
    }

    /**
     * 获取手机连接的网络类型状态 WIFI/2G/3G/4G
     */

    public static int getNetWorkStatus(Context context) {

        int netWorkType = NetWorkStatusConstants.NETWORK_CLASS_UNKNOWN;

        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
            int type = networkInfo.getType();
            if (type == ConnectivityManager.TYPE_WIFI) {
                netWorkType = NetWorkStatusConstants.NETWORK_WIFI;
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                netWorkType = getNetWorkClass(context);
            }
        }
        return netWorkType;
    }

    /**
     * 获取手机连接的网络类型是2G/3G/4G
     */

    public static int getNetWorkClass(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        switch (telephonyManager.getNetworkType()) {

            case TelephonyManager.NETWORK_TYPE_GPRS:
            case TelephonyManager.NETWORK_TYPE_EDGE:
            case TelephonyManager.NETWORK_TYPE_CDMA:
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                return NetWorkStatusConstants.NETWORK_CLASS_2_G;
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_EVDO_A:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
                return NetWorkStatusConstants.NETWORK_CLASS_3_G;
            case TelephonyManager.NETWORK_TYPE_LTE:
                return NetWorkStatusConstants.NETWORK_CLASS_4_G;
            default:
                return NetWorkStatusConstants.NETWORK_CLASS_UNKNOWN;
        }
    }
}

使用

 int netWorkStatus = NetWorkUtil.getNetWorkStatus(MainActivity.this);
 if (NetWorkStatusConstants.NETWORK_WIFI == netWorkStatus) {
                                DownloadUtil downloadUtil = new DownloadUtil(MainActivity.this);
                                downloadUtil.download(HttpUrlConstants.SERVER_RESOUSE_URL + url);
                            }else if (NetWorkStatusConstants.NETWORK_CLASS_UNKNOWN == netWorkStatus) {
                                ToastUtil.showMsg(MainActivity.this, "网络不可用,请检查网络");
                            } else {
                                dialog1 = UiUtil.createDialog(MainActivity.this, "温馨提示", "取消", "更新", "处于移动网络,是否下载!", new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                if ((boolean) view.getTag()) {
                                                    dialog1.dismiss();
                                                } else {
                                                    dialog1.dismiss();
                                                    DownloadUtil downloadUtil = new DownloadUtil(MainActivity.this);
                                                    downloadUtil.download(HttpUrlConstants.SERVER_RESOUSE_URL + url);
                                                }
                                            }
                                        }
                                );

                            }

本文由【waitig】发表在等英博客
本文固定链接:NetWorkUtil网络状态
欢迎关注本站官方公众号,每日都有干货分享!
等英博客官方公众号
点赞 (0)分享 (0)