2018-09-04

返回手机网络状态的方法

package com.henan.shuili.hhzinspectorsys.utils;

import android.content.Context;

import android.net.ConnectivityManager;

import android.net.NetworkInfo;

/**

* Created by WeiRui Kong

* on 2017/11/2.

* Company HeNan ShuiLi

* Des : 判断网络工具类

*/

public class NetUtil {

private static final int NETWORK_NONE = -1;//没有连接网络

    private static final int NETWORK_MOBILE =0;//移动网络

    private static final int NETWORK_WIFI =1;//无线网络

    /**

* 得到连接管理器对象

*

    * @param context

    * @return

    */

    public static int getNetWorkState(Context context) {

ConnectivityManager connectivityManager = (ConnectivityManager) context

.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if (activeNetworkInfo !=null && activeNetworkInfo.isConnected()) {

if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {

return NETWORK_WIFI;

            }else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {

return NETWORK_MOBILE;

            }

}else {

return NETWORK_NONE;

        }

return NETWORK_NONE;

    }

}

你可能感兴趣的:(2018-09-04)