6

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!

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • Why doing it with a Broadcast and not an Intent between apps? – Macarse May 01 '10 at 12:30
  • hmm I also tried to use Intents and then overriding the onNewIntent() method in the receiving activity, but this never worked. So i read in the Internet that if two activities are in different applications, one should use broadcast. –  May 01 '10 at 12:36

4 Answers4

14
  1. Your <receiver> element needs to be a peer of your <activity> element, not a child.
  2. Your action string should NOT be in the android.intent.action namespace, unless you work for Google -- use ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST or something like that instead
  3. 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).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    thanks a lot for you clarification! now i works! unfortunately the official documentation http://developer.android.com/guide/appendix/faq/commontasks.html#broadcastreceivers isn't that useful like your example! Thanks! – RoflcoptrException May 01 '10 at 12:55
  • Amazing answer, finally made me understand the BroadcastReceivers! – Adrian Olar Jul 24 '15 at 09:45
2

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?

Terrance
  • 11,764
  • 4
  • 54
  • 80
Nikolay
  • 21
  • 1
1

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.

Parmesh
  • 11
  • 2
0

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:

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);
dsharew
  • 10,377
  • 6
  • 49
  • 75