簡體   English   中英

android`NetworkInfo`中的`isConnected()`和`isAvailable()`之間的區別

[英]Difference between `isConnected()` and `isAvailable()` in android `NetworkInfo`

我正在嘗試檢查設備是否連接到互聯網。 我有以下實現

public static boolean isConnectedToNetwork(Context context) {
  ConnectivityManager connectivityManager =
    (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

NetworkInfo提供了兩種方法isConnected()isAvailable() 我應該使用哪一個,它們之間有什么區別。

有沒有一種方法可以在沒有互聯網連接的情況下檢測設備連接到Wifi的狀態?

如果設備連接到網絡,則isConnected返回true。 如果設備未連接但網絡可用於連接,則isAvailable返回true,isConnected返回false。

您可以閱讀本主題以找到您的最后一個問題。 Android檢查是否有WiFi但沒有互聯網

isConnected()

Indicates whether network connectivity exists and it is possible to establish connections and pass data.

- Always call this before attempting to perform data transactions.

isAvailable()

Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include

- The device is out of the coverage area for any network of this type.

- The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled.

- The device's radio is turned off, e.g., because airplane mode is enabled.

參考鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM