问题
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