Android BroadcastReceiver without intent filters

非 Y 不嫁゛ 提交于 2019-12-05 19:22:24

问题


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?


回答1:


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




回答2:


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

Create an IntentFilter in android that matches ALL intents



来源:https://stackoverflow.com/questions/11901195/android-broadcastreceiver-without-intent-filters

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