如何在 Android 的不同应用程序中使用广播接收器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2749893/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Broadcast Receiver in different Applications in Android?
提问by CommonsWare
I have here two applications in two different projects in eclipse. One application (A) defines an activity (A1) which is started first. Then i start from this activity the second activity (B1) in the second project (B). This works fine.
我在这里有两个应用程序在 eclipse 中的两个不同项目中。一个应用程序 (A) 定义了一个首先启动的活动 (A1)。然后我从这个活动开始第二个项目 (B) 中的第二个活动 (B1)。这工作正常。
I start it the following way:
我通过以下方式启动它:
Intent intent = new Intent("pacman.intent.action.Launch");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
Now i want to send intents bewtween the two activities by using broadcast receivers. In activity A1 i send the intents the following way:
现在我想通过使用广播接收器在两个活动之间发送意图。在活动 A1 中,我通过以下方式发送意图:
Intent intent = new Intent("pacman.intent.action.BROADCAST");
intent.putExtra("message","Wake up.");
sendBroadcast(intent);
The part of the manifest file in activity A1 that is responsible for this broadcast is the following:
活动 A1 中负责此广播的清单文件部分如下:
<activity android:name="ch.ifi.csg.games4blue.games.pacman.controller.PacmanGame" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.BROADCAST" />
</intent-filter>
</activity>
In the receiving activity, I define the receiver the following way in the manifest file:
在接收活动中,我在清单文件中按以下方式定义接收器:
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".PacmanGame"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="pacman.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<receiver android:name="ch.ifi.csg.games4blue.games.pacman.controller.MsgListener" />
</activity>
</application>
The class message listener is implemented this way:
类消息监听器是这样实现的:
public class MsgListener extends BroadcastReceiver {
/* (non-Javadoc)
* @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("Message at Pacman received!");
}
}
Unfortunately, the message is never received. Although the method in activity A1 is called, i never receive an intent in B1.
不幸的是,该消息从未收到。尽管调用了活动 A1 中的方法,但我从未在 B1 中收到过意图。
Any hints how to solve this? Thanks a lot!
任何提示如何解决这个问题?非常感谢!
回答by CommonsWare
- Your
<receiver>
element needs to be a peer of your<activity>
element, not a child. - Your action string should NOTbe in the
android.intent.action
namespace, unless you work for Google -- usech.ifi.csg.games4blue.games.pacman.controller.BROADCAST
or something like that instead - Your
<intent-filter>
with your custom action needs to be placed on the<receiver>
, not the sending or receiving<activity>
- 您的
<receiver>
元素必须是元素的同级<activity>
,而不是子元素。 - 你的操作字符串不应该在
android.intent.action
命名空间中,除非你为谷歌工作——使用ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST
或类似的东西来代替 - 您
<intent-filter>
的自定义操作需要放在<receiver>
,而不是发送或接收<activity>
See here for an exampleof implementing a manifest-registered broadcast receiver (for a system-broadcast Intent).
回答by Nikolay
Intent intent = new Intent("pacman.intent.action.BROADCAST");
Intent intent = new Intent("pacman.intent.action.BROADCAST");
vs.
对比
<android:name="android.intent.action.BROADCAST"/>
<android:name="android.intent.action.BROADCAST"/>
Are you sure you use the same string in real code?
您确定在实际代码中使用相同的字符串吗?
回答by Parmesh
what ever action we are passing inside in android, we have to use same action while creating Intent object or setAction() method of Intent. when we will send this Intent object with the help of sendBroadcasteReceiver() method of Context then it will send this action to all receiver(without permission), what ever receiver we have set in Manifest.xml all will(who has same action in intent-filter tag) get this action.
无论我们在android中传递什么动作,我们都必须在创建Intent对象或Intent的setAction()方法时使用相同的动作。当我们在 Context 的 sendBroadcasteReceiver() 方法的帮助下发送这个 Intent 对象时,它将将此操作发送给所有接收器(未经许可),我们在 Manifest.xml 中设置的任何接收器都将(谁在意图中具有相同的操作) -filter 标签)获取此操作。
回答by dsharew
Still not working for you?
仍然不适合你?
Though the answers are helpful I still had the a problem. I got the solution here.
尽管答案很有帮助,但我仍然遇到了问题。我在这里得到了解决方案。
when sending the broadcast add the ff flag:
发送广播时添加 ff 标志:
FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it is sent to indicate that the intent is to be allowed to start a component of a stopped application.
FLAG_INCLUDE_STOPPED_PACKAGES 标志在发送之前添加到意图中,以指示允许意图启动已停止应用程序的组件。
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);