11

Is there a publish/subscribe pattern in android?

What I want to achieve is I have this class that can notify interested party of an event. Then the interested party can do whatever it needs.

Coming from a .net microsoft world, this sort of thing are build in.

Do android have something similar, or I have to write some thing like an observer pattern?

jparthj
  • 1,606
  • 3
  • 20
  • 44
pdiddy
  • 6,217
  • 10
  • 50
  • 111
  • You can try out PServiceBus(http://pservicebus.codeplex.com/), You just need to install the .Net ESB Server and use the Java Client(https://github.com/rpgmaker/PServiceBus-Java-Client) to connect to it. With it, you can do communication between Java and any .NET app and even communication between Java and Javascript. – rpgmaker Dec 29 '11 at 00:41

5 Answers5

7

I found LocalBroadcastManager the most suitable for in app pub-sub style. You can always use observers but this one makes life more easy:

https://developer.android.com/reference/android/support/v4/content/LocalBroadcastManager.html

EDIT: It seems like there are couple of solutions nowdays. They surely worth mentioning:

ApriOri
  • 2,618
  • 29
  • 48
  • As mentioned below. For a more robust solution I would also reccomend the excellent EventBus library from greenrobot. – ApriOri Apr 11 '15 at 20:12
7

Take a look at BroadcastReceiver. I think it's what you're looking for.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • hmmm seem so tightly couple to context and intent. I have a normal class that has no knowledge of context, intent, etc. For pure example, a class responsible to counts till 100, when it finished counting till 100 it can notify other objects that are interested in this event. – pdiddy Dec 02 '11 at 20:40
  • In Android this is generally a good way to do this. Also note you can optimise by using LocalBroadcastReciver – Igor Čordaš Jul 07 '14 at 09:52
4

I think you are speaking in terms of passing events/messages among classes within your application. So this question probably goes down to Java implementation of such a pattern.

There's nothing actually already baked in (i.e. no event system class), one of the most common way to let a class spread an event/message is the Listener technique (see wikipedia, Vogel, IBM).

There are frameworks too, as from this SO answer.

If you are concerned about async messages across thread/processes in Android, then there are Handlers, AsyncTasks and (for inter-process) Parcelables.

superjos
  • 12,189
  • 6
  • 89
  • 134
3

I know this is a bit old, but it was top google result for "android publish subscribe".

Android's Local Broadcast Manager is also an option. It has some advantages over globally broadcasting intents.

It is still tightly coupled to intents though.

forforf
  • 895
  • 7
  • 14
0

I know this is old, but I have been using EventBus by GreenRobot. It is great so far. Much more loosely coupled to intents than BroadcastReceivers and LocalBroadcastManagers. Check it out here: http://greenrobot.github.io/EventBus/

dennisdrew
  • 4,399
  • 1
  • 25
  • 25