I don't understand why onAvailable is called multiple times at random moments, the documentation states that it can be called more if the network changes, but the properties of the network object parameter in onAvailable is always the same. I even compared the hashcodes and they are the same...
The problem occurs on my LG lollipop device
Android documentation: (https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback)
onAvailable Added in API level 21 Called when the framework connects and has declared a new network ready for use. This callback may be called more than once if the Network that is satisfying the request changes
EDIT: I have implemented other methods from the callback to check if any other methods are fired (onLosing, onUnavailable, onCapabilitiesChanged, onLinkPropertiesChanged), but none of these are called
I have my own NetworkManager that has this code in it:
public void initialize(Context context) {
connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
setState();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
final NetworkRequest networkRequest = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI).build();
connectivityManager.registerNetworkCallback(networkRequest, new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(final Network network) {
super.onAvailable(network);
NetworkInfo networkInfo = connectivityManager.getNetworkInfo(network);
Log.d(TAG,"Network is Available. Network Info: " + networkInfo);
notifyObservers();
}
@Override
public void onLost(final Network network) {
super.onLost(network);
notifyObservers();
}
});
} else {
context.registerReceiver(new NetworkChangeReceiver(), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
}
}
Logs:
Network is Available. Network Info: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "company_guest", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]
Network is Available. Network Info: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "company_guest", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]
Network is Available. Network Info: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "company_guest", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]
Network is Available. Network Info: [type: WIFI[], state: CONNECTED/CONNECTED, reason: (unspecified), extra: "company_guest", roaming: false, failover: false, isAvailable: true, isConnectedToProvisioningNetwork: false]
...
We encountered the same issue on the Zebra MC18 devices running Android 5.1. All our other devices, running newer Android versions, do not exhibit this behaviour.
Our workaround was to check if the last onAvailable call came from the same network and we didn't receive onLost for this network in the meantime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With