Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to have a Broadcast Receiver with No filter

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?

like image 434
jason hong Avatar asked Jan 13 '12 23:01

jason hong


People also ask

What is the difference between a broadcast receiver and an intent filter?

An IntentFilter specifies the types of intents to which an activity, service, or broadcast receiver can respond to by declaring the capabilities of a component. BroadcastReceiver does not allows an app to receive video streams from live media sources.

What are the types of broadcast receivers?

There are mainly two types of Broadcast Receivers: Static Broadcast Receivers: These types of Receivers are declared in the manifest file and works even if the app is closed. Dynamic Broadcast Receivers: These types of receivers work only if the app is active or minimized.

What are different ways of implementing broadcast receiver?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

Does broadcast receiver run on main thread?

As the previous answers correctly stated onReceive will run on the thread it's registered with if the flavour of registerReceiver() that accepts a handler is called - otherwise on the main thread.


1 Answers

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.

like image 152
John Leehey Avatar answered Sep 20 '22 18:09

John Leehey