register too many Broadcast Receivers

最近在友盟上发现一处崩溃
java.lang.IllegalArgumentException: regist too many Broadcast Receivers at android.app.ContextImpl.registerReceiverInternal
导致这个问题的原因是华为5.1和5.1.1上,有一个针对广播注册的限制,HuaWei自家定制的ROM系统中有一个白名单机制,只有加入了白名单的APP才允许注册超过500个BroadcastReceiver,否则就会抛出Register too many Broadcast Receivers的异常。
也就是说没有加入该白名单机制的APP最多只能注册500个BroadcastReceiver。

那么解决这个问题可以分成两种
1:找到注册广播的源头,解决。
2:用反射黑科技,把自己包名加入到华为白名单里,从而豁免500限制。

反射的办法网上很多个,参考
https://ishinagimoeta.github.io/2018/10/15/HuaweiRegister2many/、https://github.com/bumptech/glide/issues/1161

我们从源头排查问题,发现是glide这个图片加载框架引起的。

app打开了网络权限("android.permission.ACCESS_NETWORK_STATE"), glide 能监听网络变化,在有网、断网的时候调整自身的加载策略,核心类是DefaultConnectivityMonitorFactory、DefaultConnectivityMonitor

崩溃发生在DefaultConnectivityMonitor的注册广播函数,华为抛出的IllegalArgumentException无法被捕获

private void register() {
    if (isRegistered) {
      return;
    }

    // Initialize isConnected.
    isConnected = isConnected(context);
    try {
      // See #1405
      context.registerReceiver(connectivityReceiver,
          new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
      isRegistered = true;
    } catch (SecurityException e) {
      // See #1417, registering the receiver can throw SecurityException.
      if (Log.isLoggable(TAG, Log.WARN)) {
        Log.w(TAG, "Failed to register", e);
      }
    }
  }

好在glide支持模块自主配置,因此我们在华为的5.0、5.1上不再注册glide的网络监听事件。下面是我们解决的办法,核心是builder.setConnectivityMonitorFactory(new NoConnectivityMonitorFactory());

@GlideModule
public final class XLOkHttpLibraryGlideModule extends AppGlideModule {
    @Override
    public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
        super.applyOptions(context, builder);

        //兼容了华为平板 5.1 5.0机型上,Register too many Broadcast Receivers 的问题
        if (NoConnectivityMonitorFactory.needDisableNetCheck()) {
            builder.setConnectivityMonitorFactory(new NoConnectivityMonitorFactory());
        }
    }
}
**
 * 功能:不做网络监听,兼容华为平板
 * 描述:
 * Created by 陈俊杰 on 2019/9/3.
 */
public class NoConnectivityMonitorFactory implements ConnectivityMonitorFactory {
    /**
     * 华为 5.1 5.11机型需要禁用glide网络监听功能
     *
     * @return
     */
    public static boolean needDisableNetCheck() {
        return isHuawei() && Build.VERSION.SDK_INT < Build.VERSION_CODES.M;
    }

    static boolean isHuawei() {
        String brand = Build.BRAND.toLowerCase();
        return brand.contains("huawei") || brand.contains("honor");
    }

    @NonNull
    @Override
    public ConnectivityMonitor build(@NonNull Context context, @NonNull ConnectivityMonitor.ConnectivityListener listener) {

        return new ConnectivityMonitor() {

            @Override
            public void onStart() {
                //不做处理
            }

            @Override
            public void onStop() {
//不做处理
            }

            @Override
            public void onDestroy() {
//不做处理
            }
        };
    }
}

你可能感兴趣的:(register too many Broadcast Receivers)