我在这里有两个不同项目的应用程序在
eclipse中.一个应用程序(A)定义首先启动的活动(A1).然后我从这个活动开始第二个项目(B)中的第二个活动(B1).这很好用.
我从以下方式开始:
Intent intent = new Intent("pacman.intent.action.Launch");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
现在我想通过使用广播接收器发送两个活动之间的意图.在活动A1中,我按以下方式发送意图:
Intent intent = new Intent("pacman.intent.action.broADCAST");
intent.putExtra("message","Wake up.");
sendbroadcast(intent);
活动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>
在接收活动中,我在清单文件中按以下方式定义接收器:
<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>
类消息监听器以这种方式实现:
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!");
}
}
不幸的是,从未收到过该消息.虽然调用了活动A1中的方法,但我从未在B1中收到意图.
任何提示如何解决这个问题?
非常感谢!
解决方法
>你的<接收器> element必须是您的< activity>的同伴元素,而不是孩子.
>你的动作字符串不应该在android.intent.action名称空间中,除非你在谷歌工作 – 使用ch.ifi.csg.games4blue.games.pacman.controller.broADCAST或类似的东西
>您的< intent-filter>您的自定义操作需要放在< receiver>上,而不是发送或接收<活动>
>你的动作字符串不应该在android.intent.action名称空间中,除非你在谷歌工作 – 使用ch.ifi.csg.games4blue.games.pacman.controller.broADCAST或类似的东西
>您的< intent-filter>您的自定义操作需要放在< receiver>上,而不是发送或接收<活动>
See here for an example实现了清单注册的广播接收器(用于系统广播的Intent).