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.

posted @ 2014-08-04 14:55  feitianlee  阅读(2409)  评论(0编辑  收藏  举报