-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNetworkStateUtil.java
More file actions
75 lines (70 loc) · 2.16 KB
/
NetworkStateUtil.java
File metadata and controls
75 lines (70 loc) · 2.16 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
package com.yanxing.util;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 检测手机网络是否可用和当前网络类型
* Created by lishuangxiang on 2016/1/25.
*/
public class NetworkStateUtil {
public static final String NO_NETWORK_CONNECTED="当前网络不可用,请检查网络连接再试";
/**
* 网络是否可用
*
* @param context
* @return true 可用,false不可用
*/
public static boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
if (networkInfo != null) {
return networkInfo.isAvailable();
}
}
return false;
}
/**
* WIFI是否可用
*
* @param context
* @return true 可用,false不可用
*/
public static boolean isWifiConnected(Context context) {
if (getConnectedType(context)==ConnectivityManager.TYPE_WIFI){
return true;
}else {
return false;
}
}
/**
* 移动网络是否可用
*
* @param context
* @return true 可用,false不可用
*/
public static boolean isMobileConnected(Context context) {
if (getConnectedType(context)==ConnectivityManager.TYPE_MOBILE){
return true;
}else {
return false;
}
}
/**
* 获取网络连接类型
*
* @param context
* @return -1 获取失败
*/
public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isAvailable()) {
return networkInfo.getType();
}
}
return -1;
}
}