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.

转载于:https://www.cnblogs.com/feitianlee/p/3890162.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值