2

I have an application Application1 which contains 2 java classes Sender (for sending a Broadcast intent) and Receiver (for receiving the Broadcast intent broadcasted by Sender).

So can I receive the intent broadcasted by Sender of Application1 in a Receiver class of an other application, let suppose Applications2? Is it possible to receive the intent broadcasted by one application into an other application?

swdeveloper
  • 908
  • 1
  • 11
  • 33

1 Answers1

2

the answer is - yes, you can. BroadcastReceiver is the right (and only trivial) way passing data between applications. no problem broadcasting intent from application1 and receive it with the appropriate intent filter in application2. furthermore - Google recommends doing so. one thing you need to know - your intent filter should be unique, and not easy to guess by malicious apps want to intercept some private user's data you don't want to share with them.

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
  • Then how to do so. I have registered a receiver in Application1's Manifest file with Intent-Filter and action, then I should use that action in Application2's java code? – swdeveloper Aug 11 '12 at 12:51
  • yes. that what you need to do if you want to send broadcast to application1 from application2. you tried to do that and it didn't worked? – Tal Kanel Aug 11 '12 at 12:58
  • Actually I am broadcasting from Application1 and receiving in Application2 and did not work – swdeveloper Aug 11 '12 at 13:03
  • I have also registered a receiver in Application2's Manifest file with the same action that was given in Application1's broadcasted Intent. – swdeveloper Aug 11 '12 at 13:05
  • so application1 sending the broadcast from java code, and application2 register in the manifest with the receiver. anyway - post your relevant code. I'll have a look – Tal Kanel Aug 11 '12 at 13:05
  • I have written this code in my Sender java file of Application1 Intent intent = new Intent("example.test.MYBROADCAST"); intent.putExtra("msg", "My message"); sendBroadcast(intent); – swdeveloper Aug 11 '12 at 13:11
  • @swdeveloper: that's looks ok. now let's see application2 implementation of receiver, and manifest. did your receiver declaration in the manifest is under specific activity? – Tal Kanel Aug 11 '12 at 13:13
  • And I am receiving that Broadcasted intent in Receiver (in Application1) which is registered in Manifest (of Application1) with action "example.test.MYBROADCAST" and it works fine. But I want to receive in Application2's Receiver, which is registered in Application2's Manifest with action "example.test.MYBROADCAST" and it does not work – swdeveloper Aug 11 '12 at 13:16
  • @swdeveloper: I need to see the whole manifest, and the whole receiver implementation. because from what you are saying - everything should work – Tal Kanel Aug 11 '12 at 13:19
  • This is App1's Manifest – swdeveloper Aug 11 '12 at 13:22
  • This is application2's Manifest – swdeveloper Aug 11 '12 at 13:23
  • Actually I am receiving in both, Application1's MyBroadcastReceiver java file (which work fine) and Application2's Receiver java file (which does not work) – swdeveloper Aug 11 '12 at 13:26
  • @swdeveloper: your application2 implemts class called - Receiver, which is inside package name with the same name of your application's package name? – Tal Kanel Aug 11 '12 at 13:26
  • No the package name of Application2's class is different – swdeveloper Aug 11 '12 at 13:27
  • @swdeveloper: I have to go now. from all what you wrote - it suppose to work if class Receiver is inside package name with the same package name of your applications package name. anyway - I don't understand why you asked that question - you are saying that you succeed sending from first application to second application, but did not succeed sending from second to first. so you know it's possible – Tal Kanel Aug 11 '12 at 13:32
  • @swdeveloper: if the package name of the application is diffrent - that's explains why it doesn't work. don't write ".Receive", but the whole pakage: name="com.my.class.package.Receiver" – Tal Kanel Aug 11 '12 at 13:34
  • Please last question: Should the package name in both applications must be the same ? – swdeveloper Aug 11 '12 at 13:34
  • @swdeveloper: no, they don't. the only thing must be the same is the action name (intent filter string name..). please give me some credit for my help :) – Tal Kanel Aug 11 '12 at 13:35
  • I am trying to figure out. A lots of thanks for your suggestions. Thank you so much. – swdeveloper Aug 11 '12 at 13:39