1

On page 550 of the commonsware 4.0 book, the Messenger is discussed and it is explained how the Activity can attach extra data when starting the service that allows the service to send messages back to activity anytime it wants.

It then explains how the service can send data back to the Activity anytime it wants.

I have this implemented and currently working in my app.

However I have a need for the activity to talk to the service on other occasions - many times during the life of the activity. What tool or conduit allows the service to send messages to the Activity any time it wants?

Thanks, Gary

Snicolas
  • 37,840
  • 15
  • 114
  • 173
Dean Blakely
  • 3,535
  • 11
  • 51
  • 83

1 Answers1

1

What tool or conduit allows the service to send messages to the Activity any time it wants?

By your own admission:

It then explains how the service can send data back to the Activity anytime it wants.

Hence, your answer is Messenger. Or any of the other techniques outlined in that chapter.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • the chapter says "A Messenger sends messages to an Activity's handler. It's all about sending messages from a service to an activity. It never addresses the activity sending messages to the service other than when the service is first started. – Dean Blakely Nov 03 '12 at 22:33
  • @user1058647: Your question has nothing about "the activity sending messages to the service". To send messages to the service, at any point, use `startService()`. – CommonsWare Nov 03 '12 at 23:12
  • But that would execute the onStartCommand in the service again, no? – Dean Blakely Nov 03 '12 at 23:20
  • @user1058647: Correct. In `onStartCommand()`, you process the command, just like you did for the first `startService()`. You can call `startService()` as many times as you wish, to send as many commands as you wish. – CommonsWare Nov 03 '12 at 23:27
  • Ok, the service can test the message to see if it's a real startservice or just a startservice for purpose of a message. I suppose you have seen this http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging it looks like another way. – Dean Blakely Nov 03 '12 at 23:54
  • Your service could also start a new thread and send message to the activity from this thread using the receiver. It depends whether your service can produce the information for the activity by itself or it needs some triggering from the activity. In the later case, use @CommonsWare answer. – Snicolas Nov 03 '12 at 23:54
  • CommonsWare: I did a test and found that subsequent StartServices using the same instance of the Intent reset the state inside the service. So, using multiple startservices to communicate to the service won't work. – Dean Blakely Nov 05 '12 at 00:39
  • @user1058647: "using multiple startservices to communicate to the service won't work" -- yes, it will. Either you are the one who is "reset[ting] the state inside the service" (in which case, you need to fix your bug), or your service had been stopped in between the messages (in which case, "reset the state" is supposed to happen). You can see an example of `startService()` sending commands in the book that you cited in your question: https://github.com/commonsguy/cw-omnibus/tree/master/Service/FakePlayer – CommonsWare Nov 05 '12 at 01:34