tags:

views:

84

answers:

2

Hi, ive configured local service under some app. i wanna know how could i invoke methods on the activity, through the service in a synchronous way for example: the service invoke a method inthe activity and waiting for for result(synchronous) from it. (the method exists in the activity)

thanks.

A: 

Just call the method.

CommonsWare
i am calling the method , exaclly as you explain in your example with sendBroadcast(broadcast), but in the api it says this operarion is async, i need the service actully to wait for an answer.how i could do that?
rayman
Ah, now I understand. sendBroadcast() is async, and there is nothing you can do about that. You will need to use another mechanism if you want synchronous calls. Have the activity register a listener object with the service, and have the service call that listener object. That call will be synchronous. Just be careful about your threads -- if you're calling the listener from a background thread, the activity may need to do something to update the UI in the foreground.
CommonsWare
Do you have some smaple of this mechanism?
rayman
Yes, but it's fairly deep into a chain of tutorial exercises: http://github.com/commonsguy/cw-andtutorials/tree/master/18-LocalService/Patchy/
CommonsWare
I am learning alot from your tutorials! also following your comments from several forums! i was trying to look in those sources, you think you could guide me more specificly in them, where i could find this sync mechanism call which you made? thanks, ray.
rayman
I'm going to have to point you to the book that those tutorials come from: http://commonsware.com/AndTutorials/
CommonsWare
you pointed me to http://github.com/commonsguy/cw-andtutorials/tree/master/18-LocalService/Patchy/isnt the example of sync task from local service to it's host application, is there?
rayman
Yes, there is. Look at the service implementation, where it calls a callback supplied by the activity.
CommonsWare
A: 

I found a wierd way to do it but it works, adding listener to the prefrences while register to it in the main activity, so when the value is being channge on the main activity then the listener being trigger on the service. this way it can be sync.

rayman