File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed
utilcode/lib/src/main/java/com/blankj/utilcode/util Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -284,7 +284,7 @@ public static NetworkType getNetworkType() {
284
284
NetworkType netType = NetworkType .NETWORK_NO ;
285
285
NetworkInfo info = getActiveNetworkInfo ();
286
286
if (info != null && info .isAvailable ()) {
287
- if (info . getType () == ConnectivityManager . TYPE_ETHERNET ) {
287
+ if (isEthernet () ) {
288
288
netType = NetworkType .NETWORK_ETHERNET ;
289
289
} else if (info .getType () == ConnectivityManager .TYPE_WIFI ) {
290
290
netType = NetworkType .NETWORK_WIFI ;
@@ -336,6 +336,28 @@ public static NetworkType getNetworkType() {
336
336
return netType ;
337
337
}
338
338
339
+ /**
340
+ * Return whether using ethernet.
341
+ * <p>Must hold
342
+ * {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
343
+ *
344
+ * @return {@code true}: yes<br>{@code false}: no
345
+ */
346
+ @ RequiresPermission (ACCESS_NETWORK_STATE )
347
+ public static boolean isEthernet () {
348
+ final ConnectivityManager connectivityManager = (ConnectivityManager ) Utils .getApp ().getSystemService (Context .CONNECTIVITY_SERVICE );
349
+ final NetworkInfo ethernet = connectivityManager .getNetworkInfo (ConnectivityManager .TYPE_ETHERNET );
350
+ if (ethernet != null ) {
351
+ NetworkInfo .State state = ethernet .getState ();
352
+ if (null != state ) {
353
+ if (state == NetworkInfo .State .CONNECTED || state == NetworkInfo .State .CONNECTING ) {
354
+ return true ;
355
+ }
356
+ }
357
+ }
358
+ return false ;
359
+ }
360
+
339
361
@ RequiresPermission (ACCESS_NETWORK_STATE )
340
362
private static NetworkInfo getActiveNetworkInfo () {
341
363
ConnectivityManager cm =
You can’t perform that action at this time.
0 commit comments