如何使用广播接收器在Android的不同的应用? [英] How to use Broadcast Receiver in different Applications in Android?
问题描述
我这里有两个不同的项目中的两个应用程序在月食。一个应用(A)定义了第一次启动的活性(A1)。然后,我从这项活动的第二项活动(B1)中的第二个项目(B)开始。这工作得很好。
我启动它通过以下方式:
意向意图=新的意图(pacman.intent.action.Launch);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(意向);
现在我想通过广播接收器发送意图bewtween的两项活动。在活动A1我送的意图通过以下方式:
意向意图=新的意图(pacman.intent.action.BROADCAST);
intent.putExtra(信息,醒醒。);
sendBroadcast(意向);
清单文件中活动A1,负责该广播的部分如下:
<活动机器人:名称=ch.ifi.csg.games4blue.games.pacman.controller.PacmanGame机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
<意向滤光器>
<作用机器人:名称=android.intent.action.BROADCAST/>
&所述; /意图滤光器>
< /活性GT;
在接收活动中,我定义了接收器清单文件中按以下方式:
<应用机器人:图标=@可绘制/图标机器人:标签=@字符串/ APP_NAME>
<活动机器人:名称=。PacmanGame
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =画像>
<意向滤光器>
<作用机器人:名称=pacman.intent.action.Launch/>
<类机器人:名称=android.intent.category.DEFAULT/>
&所述; /意图滤光器>
<接收器的Android版本:NAME =ch.ifi.csg.games4blue.games.pacman.controller.MsgListener/>
< /活性GT;
< /用途>
类消息监听器的实现是这样的:
公共类MsgListener扩展的BroadcastReceiver {
/ *(非Javadoc中)
* @see android.content.BroadcastReceiver#的onReceive(android.content.Context,android.content.Intent)
* /
@覆盖
公共无效的onReceive(上下文的背景下,意图意图){
的System.out.println(消息,在吃豆子收!);
}
}
不幸的是,从来没有收到消息。虽然在活动A1的方法被调用时,我从来没有收到B1的意图。
任何提示如何解决此问题? 非常感谢!
- 您
<接收器>
元素必须是你的℃的同行,活动>
元素,而不是一个孩子。 - 您的操作字符串应该不会会在
android.intent.action
命名空间,除非你的工作对谷歌 - 使用ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST
之类的东西,而不是 - 您
<意向滤光器>
您的自定义操作需要放置在<接收器>
,不发送或接收<活性GT;
见这里的一个例子实施舱单注册的广播接收器(为系统广播意图)。
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.
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:
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:
<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.
Any hints how to solve this? Thanks a lot!
- Your
<receiver>
element needs to be a peer of your<activity>
element, not a child. - Your action string should NOT be 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>
See here for an example of implementing a manifest-registered broadcast receiver (for a system-broadcast Intent).
这篇关于如何使用广播接收器在Android的不同的应用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!