Trying to have a Broadcast Receiver with No filter

时光总嘲笑我的痴心妄想 提交于 2019-12-06 04:23:03

问题


IntentFilter intentFilter = new IntentFilter("test");
registerReceiver(mReceiver, intentFilter);

I would like to have no filter like registerReceiver(mReceiver, null) but my app crashes as a result of that. Can I have new IntentFiler() as an empty filer?


回答1:


Because the BroadcastReceiver returns null when there is no match via the criteria from the IntentFilter, it is not possible with the API to accomplish what you hope to accomplish (which I'm assuming is sending any and all Broadcasts to mReceiver).

You can certainly specify an empty IntentFilter, but this will be pretty useless as registering the receiver won't cause it to catch any broadcasts (unless they are directly targeted to the receiver, as mentioned by MisterSquonk in the comments). Otherwise you must know exactly which broadcasts you want to catch with your BroadcastReceiver, then specify the criteria in the IntentFilter.



来源:https://stackoverflow.com/questions/8858692/trying-to-have-a-broadcast-receiver-with-no-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!