forked from TonnyL/PaperPlane
/
NetworkState.java
58 lines (50 loc) · 1.87 KB
/
NetworkState.java
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
package com.marktony.zhihudaily.utils;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by lizhaotailang on 2016/3/18.
* judge the network state, whether the network is connected
* 判断当前的网络状态,是否有网络连接
* WiFi或者是移动数据
* or is wifi or mobile data
*/
public class NetworkState {
// 检查是否连接到网络
// whether connect to internet
public static boolean networkConnected(Context context){
if (context != null){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null)
return info.isAvailable();
}
return false;
}
// 检查WiFi是否连接
// if wifi connect
public static boolean wifiConnected(Context context){
if (context != null){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null){
if (info.getType() == ConnectivityManager.TYPE_WIFI)
return info.isAvailable();
}
}
return false;
}
// 检查移动网络是否连接
// if mobile data connect
public static boolean mobileDataConnected(Context context){
if (context != null){
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
if (info != null){
if (info.getType() == ConnectivityManager.TYPE_MOBILE)
return true;
}
}
return false;
}
}