zoukankan      html  css  js  c++  java
  • android.content.ReceiverCallNotAllowedException问题解决

    动态注册BroadcastReceiver时有时候会遇到这个异常:

    android.content.ReceiverCallNotAllowedException: BroadcastReceiver components are not allowed to register to receive intents

    注意以下两点:

    1. manifest下配置的broadcast receiver 内最好不好去register receiver.

    2. 自己也不要注册自己,比如 class A extends BroadcastReceiver, 然后A提供

    public class ATracker extends BroadcastReceiver {

    …………….

    ……..

    public void startTracker() {

            this.context.getApplicationContext().registerReceiver(this, filter);

        }

    …………

    ………….

    }

    注册时使用getApplicationContext()最好。

    参考资料(register函数说明最后一段):

    Note: this method cannot be called from a BroadcastReceiver component; that is, from a BroadcastReceiver that is declared in an application's manifest. It is okay, however, to call this method from another BroadcastReceiver that has itself been registered at run time with registerReceiver, since the lifetime of such a registered BroadcastReceiver is tied to the object that registered it.

  • 相关阅读:
    CentOS7.4安装Docker
    责任链模式
    策略模式
    状态模式
    解释器模式
    备忘录模式
    中介者模式
    观察者模式
    迭代器模式
    private、default、protected和public的作用域
  • 原文地址:https://www.cnblogs.com/feitianlee/p/3890162.html
Copyright © 2011-2022 走看看