0

Note: Don't mark this as duplicate as this existing answer does not solved my query android.net.ConnectivityManager$TooManyRequestsException in android while network call

Update Wed 28 Jun : Found this is happening only on Android 11 devices.

I have added a callback to notify device is connected to internet or not code is giving me following exception in firebase I have never observed this issue on the app following is the exception log

Exception android.net.ConnectivityManager$TooManyRequestsException:
at android.net.ConnectivityManager.convertServiceException 
(ConnectivityManager.java:3698)
at android.net.ConnectivityManager.sendRequestForNetwork 
(ConnectivityManager.java:3886)
at android.net.ConnectivityManager.registerDefaultNetworkCallback 
(ConnectivityManager.java:4378)
at android.net.ConnectivityManager.registerDefaultNetworkCallback 
(ConnectivityManager.java:4345)
at com.myapp.android.core.network.PieConnectionStatusWatcher.registerCallback 
(SourceFile:38)
at com.myapp.android.core.network.NetworkStateMonitor.addCallback (SourceFile:62)
at com.myapp.android.core.mvvm.repository.MyDataRepository.notifyIfAppIsOnline 
(SourceFile:161)
at com.myapp.android.banner.viewmodel.MyViewModel.startReloadIfAppIsOnlineAgain 
(SourceFile:287)
 at com.myapp.android.viewmodel.MyViewModel.onAdLoadingFailed (SourceFile:262)
 at com.myapp.android.core.mvvm.repository.MyDataRepository.lambda$loadAd$2
 at android.os.Handler.handleCallback (Handler.java:938)
 at android.os.Handler.dispatchMessage (Handler.java:99)
 at android.os.Looper.loop (Looper.java:257)
 at android.app.ActivityThread.main (ActivityThread.java:8390)
 at java.lang.reflect.Method.invoke
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run 
 (RuntimeInit.java:603)
 at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:947)

I made sure to add and remove callback as mentioned in few solutions as below

connectivityManager = requireContext().getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
connectivityManager?.registerDefaultNetworkCallback(connectionCallback)

Unregister:

connectivityManager?.unregisterNetworkCallback(connectionCallback)

I am still able to see this crash any reason how to fix this?

Condition under which I add network callback when api call fails due to network not available and unregister call either when user comes back online or activity is destroyed.

 public Object notifyIfAppIsOnline(Runnable onAppIsOnline) {
    NetworkStateMonitor.Callback connectionStateListener = new NetworkStateMonitor.Callback() {
        @Override
        public void onNetworkStateChanged(boolean isOnline) {
            if (isOnline) {
                networkStateMonitor.removeCallback(this);
                onAppIsOnline.run();
            }
        }
    };

    networkStateMonitor.addCallback(connectionStateListener);

    return connectionStateListener;
}

public void onDestroy() {
    if (appIsOnlineNotification != null) {
        myRepository.cancelAppOnlineNotification(appIsOnlineNotification);
    }
 }
amodkanthe
  • 4,345
  • 6
  • 36
  • 77
  • 1
    Can you include under what conditions you register and unregister the callback? Btw, `ConnectivityManager` tracks the number of callbacks for a particular PID. Therefore you may be correctly unregistering this callback, but not other callbacks in your application which could be one potential reason you are hitting this error. – Always Learning Jun 26 '23 at 22:00
  • Can you maybe share the full body of your `notifyIfAppIsOnline` function? – Rosário Pereira Fernandes Jun 27 '23 at 15:52
  • @AlwaysLearning updated question please check – amodkanthe Jun 28 '23 at 16:32
  • @Rosário Pereira Fernandes updated question please check – amodkanthe Jun 28 '23 at 16:32
  • Actually, I was hoping you could show your code that uses `ConnectivityManager` APIs `registerDefaultNetworkCallback` and `registerDefaultNetworkCallback`. I need to see how those are being used to better help. Also, is `NetworkStateMonitor` a custom class? If so, what triggers its `onNetworkStateChanged`? – Always Learning Jun 29 '23 at 04:24

0 Answers0