-
Notifications
You must be signed in to change notification settings - Fork 32
/
NetworkUtils.java
95 lines (78 loc) · 2.75 KB
/
NetworkUtils.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
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
85
86
87
88
89
90
91
92
93
94
95
package com.sunyata.netbus.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.provider.Settings;
import com.sunyata.netbus.NetworkManager;
import com.sunyata.netbus.type.NetType;
/**
* Created by 「孙晨」 on 2019/3/31 0031 19:04.
* <p>
* God bless me only
* <p>
* NetworkUtils
*/
@SuppressLint("MissingPermission")
public class NetworkUtils {
/**
* 网络是否可用
*/
public static boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) NetworkManager.getInstance()
.getApplication()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return false;
}
NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
if (info != null) {
for (NetworkInfo networkInfo : info) {
if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
/**
* 获取当前的网络类型
*/
public static NetType getNetType() {
ConnectivityManager connectivityManager = (ConnectivityManager) NetworkManager.getInstance()
.getApplication()
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null) {
return NetType.NONE;
}
// 获取当前激活的网络连接信息
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info == null) {
return NetType.NONE;
}
int type = info.getType();
if (type == ConnectivityManager.TYPE_MOBILE) {
if (info.getExtraInfo().toLowerCase().equals("cmnet")) {
return NetType.CMNET;
} else {
return NetType.CMWAP;
}
} else if (type == ConnectivityManager.TYPE_WIFI) {
return NetType.WIFI;
}
return NetType.NONE;
}
/**
* 打开网络设置界面
*/
public static void openSetting(Context context, int requestCode) {
// Intent intent = new Intent("/");
// ComponentName name = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
// intent.setComponent(name);
// intent.setAction("android.intent.action.VIEW");
// ((Activity) context).startActivityForResult(intent, requestCode);
// context.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
context.startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
}