0

I have seen multiple solutions to let a Service / Activity communicate each other. However I can't understand in which context we should use them and why. From what I've read around (android documentation and stackoverflow), these are the main approaches:

  1. Bind to the service using bindService()
  2. Use a messenger (it should be for IPC, but somebody uses it for same process communication)
  3. Static instance (singleton) of the Service. This method could bring errors, what happens if the service dies? Null reference of that service. Shouldn't be better the first approach then this one?
  4. Use LocalBroadcastManager and register the Activity for receiving. Basically, the Activity listen to the broadcasts sent by the Service. Viceversa, the Service in its onStartCommand dispatch intent actions sent from the Activity (explicit intent with custom action). For dispatching actions in the service, another solution could be intent filters on the service and implicit intents (android documentation says NO in the notes, even if I declare custom actions). How many chances are that someone else uses the same custom actions as mine?
  5. Otto API
  6. Pending Intent as per documentation where it says:

If the service does not also provide binding, the intent delivered with startService() is the only mode of communication between the application component and the service. However, if you want the service to send a result back, then the client that starts the service can create a PendingIntent for a broadcast (with getBroadcast()) and deliver it to the service in the Intent that starts the service. The service can then use the broadcast to deliver a result.

Now I'm really confused. Could somebody explain (if possible with examples) why and in which context we should use those approaches? Are there other solutions other than the ones in this list? Thank you.

Community
  • 1
  • 1
Angelo
  • 907
  • 3
  • 16
  • 33
  • This question is soliciting opinions. That isn't what Stackoverflow is here for. This forum is not a discussion forum. We are here to answer specific programming questions. – David Wasser Feb 13 '15 at 11:11
  • @DavidWasser thanks for your comment. Actually, I don't think it's opinions: it's a summary in order to understand the use case of each of the approaches. I'm stuck as I don't know if, in my app, I should go on with binders or if LocalBroadcastManager is a better pattern. – Angelo Feb 13 '15 at 14:32
  • Sorry, I can't agree. You've asked "which one is best approach". This is soliciting opinions. Also, 10 people will give you 14 different answers to this question. I'm sure your question is a valid and important one for you. It just doesn't belong on Stackoverflow. There are other forums where you can discuss this until everyone is blue in the face. – David Wasser Feb 13 '15 at 15:06
  • @DavidWasser [Opinion](http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android) as well. – Angelo Feb 13 '15 at 15:18

0 Answers0