Reputation:
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!
Upvotes: 6
Views: 31483
Reputation: 10665
Though the answers are helpful I still had the a problem. I got the solution here.
when sending the broadcast add the ff flag:
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.
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Upvotes: 0
Reputation: 11
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.
Upvotes: 1
Reputation: 21
Intent intent = new Intent("pacman.intent.action.BROADCAST");
vs.
<android:name="android.intent.action.BROADCAST"/>
Are you sure you use the same string in real code?
Upvotes: 2
Reputation: 1006614
<receiver>
element needs to be a peer of your <activity>
element, not a child.android.intent.action
namespace, unless you work for Google -- use ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST
or something like that instead<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).
Upvotes: 14