返回手機(jī)網(wǎng)絡(luò)狀態(tài)的方法
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 : 判斷網(wǎng)絡(luò)工具類
*/
public class NetUtil {
private static final int NETWORK_NONE = -1;//沒有連接網(wǎng)絡(luò)
? ? private static final int NETWORK_MOBILE =0;//移動網(wǎng)絡(luò)
? ? private static final int NETWORK_WIFI =1;//無線網(wǎng)絡(luò)
? ? /**
* 得到連接管理器對象
*
? ? * @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;
? ? }
}