|
7 | 7 | import android.net.wifi.WifiManager; |
8 | 8 | import android.telephony.TelephonyManager; |
9 | 9 |
|
10 | | -import java.lang.reflect.Field; |
11 | | -import java.lang.reflect.InvocationTargetException; |
12 | 10 | import java.lang.reflect.Method; |
13 | 11 | import java.net.InetAddress; |
14 | 12 | import java.net.NetworkInterface; |
|
20 | 18 | import java.util.concurrent.ExecutorService; |
21 | 19 | import java.util.concurrent.Executors; |
22 | 20 | import java.util.concurrent.Future; |
| 21 | +import java.util.concurrent.TimeUnit; |
| 22 | +import java.util.concurrent.TimeoutException; |
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * <pre> |
@@ -73,6 +73,18 @@ private static NetworkInfo getActiveNetworkInfo(Context context) { |
73 | 73 | return cm.getActiveNetworkInfo(); |
74 | 74 | } |
75 | 75 |
|
| 76 | + /** |
| 77 | + * 判断网络是否连接 |
| 78 | + * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p> |
| 79 | + * |
| 80 | + * @param context 上下文 |
| 81 | + * @return {@code true}: 是<br>{@code false}: 否 |
| 82 | + */ |
| 83 | + public static boolean isConnected(Context context) { |
| 84 | + NetworkInfo info = getActiveNetworkInfo(context); |
| 85 | + return info != null && info.isConnected(); |
| 86 | + } |
| 87 | + |
76 | 88 | /** |
77 | 89 | * 判断网络是否可用 |
78 | 90 | * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p> |
@@ -94,7 +106,6 @@ public static boolean isAvailableByPing(Context context) { |
94 | 106 |
|
95 | 107 | /** |
96 | 108 | * 判断移动数据是否打开 |
97 | | - * <p>需系统应用 需添加权限{@code <uses-permission android:name="android.permission.MODIFY_PHONE_STATE"/>}</p> |
98 | 109 | * |
99 | 110 | * @param context 上下文 |
100 | 111 | * @return {@code true}: 是<br>{@code false}: 否 |
@@ -119,7 +130,7 @@ public static boolean getDataEnabled(Context context) { |
119 | 130 | * @param context 上下文 |
120 | 131 | * @param enabled {@code true}: 打开<br>{@code false}: 关闭 |
121 | 132 | */ |
122 | | - private void setDataEnabled(Context context, boolean enabled) { |
| 133 | + public static void setDataEnabled(Context context, boolean enabled) { |
123 | 134 | try { |
124 | 135 | TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
125 | 136 | Method setMobileDataEnabledMethod = tm.getClass().getDeclaredMethod("setDataEnabled", boolean.class); |
@@ -131,18 +142,6 @@ private void setDataEnabled(Context context, boolean enabled) { |
131 | 142 | } |
132 | 143 | } |
133 | 144 |
|
134 | | - /** |
135 | | - * 判断网络是否连接 |
136 | | - * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p> |
137 | | - * |
138 | | - * @param context 上下文 |
139 | | - * @return {@code true}: 是<br>{@code false}: 否 |
140 | | - */ |
141 | | - public static boolean isConnected(Context context) { |
142 | | - NetworkInfo info = getActiveNetworkInfo(context); |
143 | | - return info != null && info.isConnected(); |
144 | | - } |
145 | | - |
146 | 145 | /** |
147 | 146 | * 判断网络是否是4G |
148 | 147 | * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p> |
@@ -155,23 +154,9 @@ public static boolean is4G(Context context) { |
155 | 154 | return info != null && info.isAvailable() && info.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE; |
156 | 155 | } |
157 | 156 |
|
158 | | - /** |
159 | | - * 判断wifi是否连接状态 |
160 | | - * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p> |
161 | | - * |
162 | | - * @param context 上下文 |
163 | | - * @return {@code true}: 连接<br>{@code false}: 未连接 |
164 | | - */ |
165 | | - public static boolean isWifiConnected(Context context) { |
166 | | - ConnectivityManager cm = (ConnectivityManager) context |
167 | | - .getSystemService(Context.CONNECTIVITY_SERVICE); |
168 | | - return cm != null && cm.getActiveNetworkInfo() != null |
169 | | - && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; |
170 | | - } |
171 | | - |
172 | 157 | /** |
173 | 158 | * 判断wifi是否打开 |
174 | | - * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> |
| 159 | + * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p> |
175 | 160 | * |
176 | 161 | * @param context 上下文 |
177 | 162 | * @return {@code true}: 是<br>{@code false}: 否 |
@@ -183,7 +168,7 @@ public static boolean getWifiEnabled(Context context) { |
183 | 168 |
|
184 | 169 | /** |
185 | 170 | * 打开或关闭wifi |
186 | | - * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> |
| 171 | + * <p>需添加权限 { <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>}</p> |
187 | 172 | * |
188 | 173 | * @param context 上下文 |
189 | 174 | * @param enabled {@code true}: 打开<br>{@code false}: 关闭 |
@@ -201,6 +186,43 @@ public static void setWifiEnabled(Context context, boolean enabled) { |
201 | 186 | } |
202 | 187 | } |
203 | 188 |
|
| 189 | + /** |
| 190 | + * 判断wifi是否连接状态 |
| 191 | + * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>}</p> |
| 192 | + * |
| 193 | + * @param context 上下文 |
| 194 | + * @return {@code true}: 连接<br>{@code false}: 未连接 |
| 195 | + */ |
| 196 | + public static boolean isWifiConnected(Context context) { |
| 197 | + ConnectivityManager cm = (ConnectivityManager) context |
| 198 | + .getSystemService(Context.CONNECTIVITY_SERVICE); |
| 199 | + return cm != null && cm.getActiveNetworkInfo() != null |
| 200 | + && cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI; |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * 判断wifi数据是否可用 |
| 205 | + * <p>需添加权限 {@code <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>}</p> |
| 206 | + * <p>需添加权限 {@code <uses-permission android:name="android.permission.INTERNET"/>}</p> |
| 207 | + * |
| 208 | + * @param context 上下文 |
| 209 | + * @return {@code true}: 是<br>{@code false}: 否 |
| 210 | + */ |
| 211 | + public static boolean isWifiAvailable(Context context) { |
| 212 | + return getWifiEnabled(context) && isAvailableByPing(context); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * 获取网络运营商名称 |
| 217 | + * <p>中国移动、如中国联通、中国电信</p> |
| 218 | + * |
| 219 | + * @param context 上下文 |
| 220 | + * @return 运营商名称 |
| 221 | + */ |
| 222 | + public static String getNetworkOperatorName(Context context) { |
| 223 | + TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 224 | + return tm != null ? tm.getNetworkOperatorName() : null; |
| 225 | + } |
204 | 226 |
|
205 | 227 | /** |
206 | 228 | * 获取当前的网络类型(WIFI,2G,3G,4G) |
@@ -217,7 +239,7 @@ public static void setWifiEnabled(Context context, boolean enabled) { |
217 | 239 | * <li>{@link #NETWORK_NO } = -1;</li> |
218 | 240 | * </ul> |
219 | 241 | */ |
220 | | - public static int getNetWorkType(Context context) { |
| 242 | + public static int getNetworkType(Context context) { |
221 | 243 | int netType = NETWORK_NO; |
222 | 244 | NetworkInfo info = getActiveNetworkInfo(context); |
223 | 245 | if (info != null && info.isAvailable()) { |
@@ -287,8 +309,8 @@ public static int getNetWorkType(Context context) { |
287 | 309 | * <li>NETWORK_NO </li> |
288 | 310 | * </ul> |
289 | 311 | */ |
290 | | - public static String getNetWorkTypeName(Context context) { |
291 | | - switch (getNetWorkType(context)) { |
| 312 | + public static String getNetworkTypeName(Context context) { |
| 313 | + switch (getNetworkType(context)) { |
292 | 314 | case NETWORK_WIFI: |
293 | 315 | return "NETWORK_WIFI"; |
294 | 316 | case NETWORK_4G: |
@@ -356,14 +378,14 @@ public String call() throws Exception { |
356 | 378 | return inetAddress.getHostAddress(); |
357 | 379 | } catch (UnknownHostException e) { |
358 | 380 | e.printStackTrace(); |
359 | | - return null; |
360 | 381 | } |
| 382 | + return null; |
361 | 383 | } |
362 | 384 | }); |
363 | 385 | return fs.get(); |
364 | 386 | } catch (InterruptedException | ExecutionException e) { |
365 | 387 | e.printStackTrace(); |
366 | | - return null; |
367 | 388 | } |
| 389 | + return null; |
368 | 390 | } |
369 | 391 | } |