我在 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 中收到意图。
任何提示如何解决这个问题?非常感谢!