安卓的BroadcastReceiver没有意图过滤器过滤器、意图、BroadcastReceiver

2023-09-06 02:23:39 作者:蒾亂の吢何埘才媞烬頭

我遇到这样的事情在AndroidManifest.xml:

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

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

以上是TestReceiver扩展的BroadcastReceiver类。我认为,接收器将接收所有的意图,但显然它没有,它不工作,除非我添加的意图过滤器标签了。那么它有什么作用,如果它没有意图过滤器?它是一个笔误还是真的做点什么?

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?

更新:我想通了这一点与这个链接的帮助Trying有广播接收器与无过滤

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

,你可以设置一个动作字符串的意图,然后播放它。样品code,以供参考:

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);

在的BroadcastReceiver的处理是相同的。

The handling at the BroadcastReceiver is the same.

推荐答案

这是意图过滤器是必要的情况下隐含的意图的,如​​果没有指定一个意图过滤器,就必须显式调用。因此,要调用此接收器,你将需要调用:

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);`