anon
anon

Reputation:

How to use Broadcast Receiver in different Applications in Android?

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

Answers (4)

dsharew
dsharew

Reputation: 10665

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);

Upvotes: 0

Parmesh
Parmesh

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

Nikolay
Nikolay

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

CommonsWare
CommonsWare

Reputation: 1006614

  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).

Upvotes: 14

Related Questions