Android 牛轧糖:提交的网络请求过多

Android Nougat: Too many NetworkRequests filed

在 Android Nougat 7.1.1 上持续使用应用程序后,它开始在后台崩溃,直到手动启动。从 Firebase 我得到了以下日志:

Caused by java.lang.IllegalArgumentException: Too many NetworkRequests filed
android.os.Parcel.readException (Parcel.java:1688)
android.os.Parcel.readException (Parcel.java:1637)
android.net.IConnectivityManager$Stub$Proxy.pendingListenForNetwork (IConnectivityManager.java:2460)
android.net.ConnectivityManager.registerNetworkCallback (ConnectivityManager.java:3127)
com.XXX.XX.location.GPSControl$Companion.setInitialized (GPSControl.java)
__null__.getGApiClient (GPSControl.java)
__null__.setGApiClient (GPSControl.java)
__null__.setConnMan (GPSControl.java)
__null__.setSf (GPSControl.java)
__null__.init (GPSControl.java)
__null__.getPI (GPSControl.java)
__null__.triggerLocation (GPSControl.java)
__null__.triggerLocation$default (GPSControl.java)
__null__.access$getInitialized$p (GPSControl.java)
com.XXX.XX.KGL.afterLoginSetup (KGL.java)
com.XXX.XX.KGL.onCreate (KGL.java)
android.app.Instrumentation.callApplicationOnCreate (Instrumentation.java:1025)
android.app.ActivityThread.handleBindApplication (ActivityThread.java:5460)
android.app.ActivityThread.-wrap2 (ActivityThread.java)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1556)
android.os.Handler.dispatchMessage (Handler.java:102)
android.os.Looper.loop (Looper.java:154)
android.app.ActivityThread.main (ActivityThread.java:6176)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:888)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:778)

我检查了 android marshmallow source code, it doesn't contain code, that throws this error, but master branch does,所以我们可以断定这个错误是牛轧糖特有的。

    private void enforceRequestCountLimit() {
        synchronized (mUidToNetworkRequestCount) {
            int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1;
            if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { //MAX_NETWORK_REQUESTS_PER_UID == 100
                throw new IllegalArgumentException("Too many NetworkRequests filed");
            }
            mUidToNetworkRequestCount.put(mUid, networkRequests);
        }
    }

请求计数器多久重置一次?

当当前网络请求数接近限制时,如何减少 activity?

问题评论中引用@CommonsWare:

根据我对源代码的阅读,您可能会从没有相应的 unregisterNetworkCallback() 调用的太多 registerNetworkCallback() 调用中得到这个。确保您正在清理这些回调。 – CommonsWare 2017 年 1 月 31 日 13:06