reixa
reixa

Reputation: 7011

How does ConnectivityManager determine if there's connectivity on the current network

You can check if there's connectivity on Android by using this snippet as stated here

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork.isConnectedOrConnecting();

But what data does ConnectivityManager need to state that the device is currently connected and functional?

As seen on the AOSP source code for ConnectivityManager.java file NetworkInfo it´s just a container of the information that ConnectivityManager builds. But ConnectivityManager gets all the data from a Interface called IConnectivityManager. I´ve lost the trace here.

public NetworkInfo getActiveNetworkInfo() {
    try {
        return mService.getActiveNetworkInfo();
    } catch (RemoteException e) {
        return null;
    }
}

Can someone throw a little light here?

Upvotes: 0

Views: 413

Answers (1)

Mattia Maestrini
Mattia Maestrini

Reputation: 32780

ConnectivityManager calls getActiveNetworkInfo of ConnectivityService:

@Override
public NetworkInfo getActiveNetworkInfo() {
    enforceAccessPermission();
    final int uid = Binder.getCallingUid();
    NetworkState state = getUnfilteredActiveNetworkState(uid);
    return getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid);
}
  • It calls getUnfilteredActiveNetworkState[1]
  • Then calls getDefaultNetwork [2]
  • Then get the NetworkAgentInfo from the SparseArray mNetworkForRequestId [3]

Upvotes: 1

Related Questions