Sebastian Nowak
Sebastian Nowak

Reputation: 5717

Android BroadcastReceiver without intent filters

I saw in few android ad networks sdks that they are declaring BroadcastReceiver with no intent filters. Something like this:

<receiver android:name="com.example.SampleReceiver" />

My guess is that such receiver would capture all possible events. So I've tried doing it myself and created a SampleReceiver:

public class SampleReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        System.out.println("Event captured: " + intent.getAction());
    }
}

I've launched the app, tried to fire some events by doing various action on my phone and noticed that onReceive() wasn't called even once.

So the question is - how does such BroadcastReceiver without intent filters work? Maybe it require the intent filters to be created via code? If so, how? If not, then why isn't it receiving any events? What's going on here?

Upvotes: 16

Views: 6972

Answers (2)

Chris
Chris

Reputation: 348

If you do not have some intent filters, the only way to receive something is to call the receiver explicitly. This would look like this:

context.sendBroadcast(new Intent(context, MyBroadcastReceiverClass.class));

Another guy already answered this question in the following post: https://stackoverflow.com/questions/10051256/broadcast-receiver-not-receiving

Upvotes: 17

wojciii
wojciii

Reputation: 4325

I think that the following question/answer should give you some clues:

Create an IntentFilter in android that matches ALL intents

Upvotes: 1

Related Questions