2017-02-24 2 views
1

기기가 인터넷에 연결되어 있는지 확인하려고합니다. 나는android`NetworkInfo`의`isConnected()`와`isAvailable()`의 차이점

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에 연결된 상태를 감지 할 수있는 방법이 있습니까?

+0

당신은 [문서]를 체크 한 (https://developer.android.com/reference/android/net/NetworkInfo.html)? – aandis

+0

제대로하고 있습니다. –

+0

@aandis 예'isConnected()'- 네트워크 연결이 존재하는지 여부를 나타냅니다. isAvailable() - 네트워크 연결이 가능한지 여부를 나타냅니다. 둘 다 똑같아 보입니다. 정확한 차이점과 용도를 알아야합니다. – arjun

답변

2

장치가 네트워크에 연결되어 있으면 isConnected는 true를 반환합니다. 장치가 연결되어 있지 않지만 네트워크에 연결할 수있는 경우 isAvailable은 true를 반환하고 isConnected는 false를 반환합니다.

이 항목을 읽으면 마지막 질문을 찾을 수 있습니다. Android Check if there is WiFi but no internet

0
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. 

Reference Link