NetworkUtils.java
1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.huaheng.download;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
/**
* Created by youjie on 2018/4/2.
*/
public class NetworkUtils {
/*
** 判断当前网络是可用并且网络良好
*/
public static boolean isGoodNetworkConnected(Context context) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
WifiManager mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int wifiState = mWifiManager.getWifiState();
if(wifiState == WifiManager.WIFI_STATE_ENABLED) {
NetworkInfo wifiInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifiInfo.isAvailable() && wifiInfo.isConnected()) {
return true;
}
}
TelephonyManager mgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = mgr.getSimState();
if (simState == TelephonyManager.SIM_STATE_READY) {
NetworkInfo info = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (info.getState() == NetworkInfo.State.CONNECTED) {
if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
|| info.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA
|| info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
return false; // 2g
}
return true;
}
}
NetworkInfo info = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
if (info!=null&&info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
return false;
}
}