安卓的BroadcastReceiver没有意图过滤器 [英] Android BroadcastReceiver with no intent-filter

查看:121
本文介绍了安卓的BroadcastReceiver没有意图过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到这样的事情在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?

更新:我想通了这一点与这个链接的帮助<一href="http://stackoverflow.com/questions/8858692/trying-to-have-a-broadcast-receiver-with-no-filter">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);`

这篇关于安卓的BroadcastReceiver没有意图过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆