Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BroadcastReceiver with no intent-filter

Tags:

android

I've come across something like this in the AndroidManifest.xml:

<receiver android:name="com.testco.test.TestReceiver"/>

The above is TestReceiver extends the BroadcastReceiver class. I thought the receiver will receive all intents but apparently it doesn't, and it doesn't work unless I add intent-filter tags in it. So what does it do if it has no intent-filter? Is it a typo or does it really do something?

UPDATE: I figured this out with the help of this link Trying to have a Broadcast Receiver with No filter

Instead of calling a broadcast with the usual String identifier, you can set an action string to the intent, then broadcast it. Sample code for reference:

Intent notifyIntent = new Intent(getApplicationContext(), TestReceiver.class);
notifyIntent.setAction("RECEIVE");
sendBroadcast(notifyIntent);

The handling at the BroadcastReceiver is the same.

like image 280
Maurice Avatar asked Jan 30 '12 06:01

Maurice


People also ask

Which intent is used by BroadcastReceiver?

A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as a Intent object parameter.

How do I know if BroadcastReceiver is registered Android?

Currently there is no way to check if a receiver is registered using the receiver reference. You have to unregister the receiver and catch the IllegalArgumentException that is thrown if it's not registered.

What is the role of the onReceive () method in the BroadcastReceiver?

Retrieve the current result extra data, as set by the previous receiver. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function.

What are the types of broadcast receivers in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.


2 Answers

An Intent filter is needed in case of implicit intents, and if an intent filter is not specified, it must be invoked explicitly. So to invoke this receiver you would need to invoke:

Intent intent = new Intent(getApplicationContext(), com.testco.test.TestReceiver.class);
sendBroadcast(intent);`
like image 117
jeet Avatar answered Sep 27 '22 18:09

jeet


From the documentation:

android:exported: Whether or not the broadcast receiver can receive messages from sources outside its application — "true" if it can, and "false" if not. If "false", the only messages the broadcast receiver can receive are those sent by components of the same application or applications with the same user ID.

The default value depends on whether the broadcast receiver contains intent filters. The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name. This implies that the receiver is intended only for application-internal use (since others would not normally know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the broadcast receiver is intended to receive intents broadcast by the system or other applications, so the default value is "true".

So the receiver will only catch Intents broadcast by your application.

like image 24
Vikram Bodicherla Avatar answered Sep 27 '22 18:09

Vikram Bodicherla