How to use the broadcast receiver in different applications on Android?

I have two applications in two different projects in eclipse. In one appendix (A), action (A1) is defined that starts first. Then I start with this activity the second activity (B1) in the second project (B). It works great.

I start it as follows:

Intent intent = new Intent("pacman.intent.action.Launch"); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); startActivity(intent); 

Now I want to send intentions before two actions using broadcast receivers. In action A1, I send intentions as follows:

 Intent intent = new Intent("pacman.intent.action.BROADCAST"); intent.putExtra("message","Wake up."); sendBroadcast(intent); 

The part of the manifest file in action A1 that is responsible for this translation is as follows:

 <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 recipient as follows 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 as follows:

 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 action A1 is called, I never get the intention in B1.

Any clues how to solve this? Many thanks!

+6
android broadcastreceiver
source share
4 answers
  • Your <receiver> element should be a peer element of your <activity> , not a child.
  • Your action bar should NOT be in the android.intent.action namespace, if you are not working on Google - use ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST or something like that instead
  • Your <intent-filter> with your custom actions should be placed on <receiver> , and not on sending or receiving <activity>

See here an example implementation of a receiver registered with a manifest (for the intent of system broadcasting).

+14
source share

Intent intent = new Intent("pacman.intent.action.BROADCAST");

against.

<android:name="android.intent.action.BROADCAST"/>

Are you sure you are using the same line in real code?

+2
source share

no matter what action we take inside android, we must use the same action when creating the Intent object or the SetAction () Intent method. when we send this Intent using the Context method sendBroadcasteReceiver (), it will send this action to the entire recipient (without permission), no matter what receiver we set in Manifest.xml (who has the same action with the -filter tag intent ) perform this action.

+1
source share

Still not working for you?

Although the answers are helpful, I still had a problem. I got a solution here .

when sending a broadcast add the ff flag:

The FLAG_INCLUDE_STOPPED_PACKAGES flag is added to the intent before it is sent to indicate that the intent should be allowed to start the stopped application component.
 intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); 
0
source share

All Articles