-4

I read this article to understand the difference between implicit and explicit broadcast. After going through this I'm getting confused with the concept of broadcast itself.

In general, the term broadcast means to scatter/announce the information to a wider audience and whoever listens/receives the message can make use of it.

In the case of explicit broadcast when we know the component name (package name according to Java terms) and activity name (corresponding class name in Java terms) why shouldn't we invoke the Class.startActivity() directly instead of configuring it to the manifest - delivering it to aosp and gaining the control of method to be invoked. I feel the purpose of the term broadcast itself is not satisfied here since we know to whom we are going to send (1-1).

Why does AOSP introduced broadcast when we have the direct control of invoking a method in Java? Is this to bring structure to an application - something like this?

EDIT :

I should have asked it more specifically like why do we need a broadcast when we are under the same process.

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
  • 2
    You could read the [Broadcasts overview](https://developer.android.com/guide/components/broadcasts) provided by google themselves, too. – deHaar Apr 10 '19 at 12:20
  • Yeah, read that too. – Tom Taylor Apr 10 '19 at 12:21
  • 1
    You need to understand that you can have N subscribers. In this way code logic can decide when to subscribe and listen to some specific events and when not. The same published event can be captured anywhere in your code and outside of it by other applications. More info: https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern – MatPag Apr 10 '19 at 12:23
  • This phrase is wrong: "I feel the purpose of the term broadcast itself is not satisfied here since we know to whom we are going to send (1-1)" publish-subscribe pattern is mainly used when you DON'T know who will receive the message, which can be 0 or N subscribers – MatPag Apr 10 '19 at 12:26
  • I'm sorry for my misunderstanding. One more question please. When we do not know who is going to receive the broadcast message - then it's okay to have such configuration. When it's inside my own app (in the case of explicit intent) - where I would know which activity of mine to be invoked - in such case why am I asking android to do this job? – Tom Taylor Apr 10 '19 at 12:34

1 Answers1

1

I feel the purpose of the term broadcast itself is not satisfied here since we know to whom we are going to send (1-1).

The original general case of broadcast Intents (implicit broadcast) was a "true" broadcast, where an arbitrary number of apps can register to listen for the broadcast. The scenario where you specify a ComponentName or package name is a specialized subset of the broadcast IPC mechanism.

Why does AOSP introduced broadcast when we have the direct control of invoking a method in Java?

Broadcasts are one form of IPC for Android. "direct control of invoking a method" is not IPC, as you cannot invoke a method in another app. For communication within your own app, you do not need broadcasts. For communications between apps, you need IPC, and for that, broadcasts are one option.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • "For communication within your own app, you do not need broadcasts" - You don't need them but in some cases you can simplify the in-app communication with local broadcasts (or event bus libraries) – MatPag Apr 10 '19 at 12:31
  • 1
    @MatPag: Agreed, though the question would appear to be about system broadcasts. – CommonsWare Apr 10 '19 at 12:33
  • One more ques please, what is the use of broadcast between the same process (with the same app)? – Tom Taylor Apr 10 '19 at 12:45
  • @rm-rfstar: As with communications between apps, communications between processes in a single app is still IPC (inter-process communication). There are a handful of options for that: starting activities, starting/binding to services, `ContentProvider`, and broadcasts are the most common. – CommonsWare Apr 10 '19 at 12:49
  • Not communication between process I'm worried about "communication within the same app/process". – Tom Taylor Apr 10 '19 at 12:53
  • @rm-rfstar: Oh, sorry. Google offers `LocalBroacastManager` as an event bus that uses the same sort of `Intent` and `IntentFilter` objects as you might use with system events. On the whole, the event bus pattern is not that popular today, and even if you wanted one, I would lean towards greenrobot's EventBus, as it is more powerful and flexible. – CommonsWare Apr 10 '19 at 13:06