-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetConnectUtils.java
More file actions
84 lines (77 loc) · 2.67 KB
/
NetConnectUtils.java
File metadata and controls
84 lines (77 loc) · 2.67 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import java.util.List;
public class NetConnectUtils {
/**
* 检测网络是否连接
*
* @return
*/
public static boolean isNetConnected(Context context) {
//1、获取ConnectivityManager对象
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null) {
//2、获取NetworkInfo对象
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
//3、判断当前网络状态是否为连接状态
if (info != null && info.isConnected()) {
// 当前网络是连接的
if (info.getState() == NetworkInfo.State.CONNECTED) {
// 当前所连接的网络可用
return true;
}
}
}
return false;
}
/**
* 检测wifi是否连接
*
* @return
*/
public boolean isWifiConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
}
return false;
}
/**
* 检测3G是否连接
*
* @return
*/
public boolean is3gConnected(Context context) {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm != null) {
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null
&& networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
return true;
}
}
return false;
}
/**
* 检测GPS是否打开
*
* @return
*/
public static boolean isGpsEnabled(Context context) {
LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
List<String> accessibleProviders = lm.getProviders(true);
for (String name : accessibleProviders) {
if ("gps".equals(name)) {
return true;
}
}
return false;
}
}