Search
Search Menu

How many NetworkCallbacks are too many? Looking at the TooManyRequestsException

While working on an android app for a client which has to do with Wifi connections, we noticed multiple crashes that showed this error:

Java:

Fatal Exception: java.lang.IllegalArgumentException
Too many NetworkRequests filed

Kotlin:

android.net.ConnectivityManager$TooManyRequestsException

A quick search tells me, that this is caused by registering callbacks to the ConnectivityManager without unregistering those. But I couldn’t reproduce this crash during my normal development work and short debugging sessions. So clearly, this issue occurs only when the app is running for a long time and a lot of NetworkCallbaks are registered without unregistering. The question is: How many callbacks are to many?

The answer is 100. I created an example project to test this, where I can register a NetworkCallback on a button click and count how many callbacks are already registered. The app crashes every time, once the counter reaches 100 (tested on Android 8.0 and 10.0).
Side note: It doesn’t matter if you create a new NetworkCallback or just reuse the same NetworkCallback object again (which will cause the following message in your logs: NetworkCallback was already registered).

fun registerCallback(view: View) {
    val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkCallback = ConnectivityManager.NetworkCallback()
    connectivityManager.registerNetworkCallback(wifiNetworkRequest, networkCallback)

    registeredCallbacks++
    callbackCounter.text = getString(R.string.callbacksRegistered, registeredCallbacks)
}

Now you know it. But despite the hard limit of 100 registered callbacks, you probably shouldn’t need more than one NetworkCallback registered at any given time.

Author:

Mobile developer located in Düsseldorf, Germany.

Leave a Comment

Required fields are marked *.


This site uses Akismet to reduce spam. Learn how your comment data is processed.