Hi,

I have a similar question. My application has a long running service
that acts as a TCP client. There is also an activity which needs to be
able to pass messages to the service which will then send them to the
TCP server. In turn, the service needs to 'forward' the reply it gets
from the TCP Server to the activity, which performs an action as a
result.

I understand there are [at least] two ways to deal with this:

1. Broadcast receivers and intents: The service and activity each
register a broadcast receiver, and broadcast intents to communicate
with each other, using putExtras for the data.

2. IPC using AIDL. I define two interfaces (activity to service, and
service to activity), make my TCP message class implement parcelable.
When my activity starts up, I bind to the service (if running, or
create it if not), then receive the Service->Activity aidl Stub, then
use a method defined within that to pass the Activity->Service Stub.
Service and client now both have IPC Stubs and can call the each
other's methods that have been defined in the interfaces.

So my questions:
i) is the above roughly correct?
ii) are there any other sensible ways to do it? (e.g. I have heard the
term Local binder / local binding pattern in the last post - is that
the same as [2]?)
iii) which is most appropriate for my situation?

Any help would be much appreciated - this is really confusing me.

Cheers

~redders.

On May 3, 1:02 pm, Mark Murphy <[email protected]> wrote:
> FrankG wrote:
> > I have short question to your example :
>
> > If I understood it right, the Broadcast from the WeatherPlusService
> > will not start
> > up the WeatherPlus Activity ?
>
> Correct. In this case, the WeatherPlus activity is already running.
>
> > So how must the example be changed, that the first broadcast will
> > startup the activity
> > and the next ones are normal broadcast to send the new status to the
> > activity.
>
> > I know that I can use startActivity(Intent) to startup the
> > activity ..and use then sendBroadcast(Intent),
> > but you cannot know, whether then your Activity is still running.
>
> Sure you can, though the sendBroadcast() model is probably not the right
> one to use.
>
> Have the activity bind to the service using the local binding pattern
> and have it register a callback/listener object with the service once bound.
>
> When the "devise status changed", the service looks to see if it has a
> listener from an activity.
>
> -- If yes, it calls a method on that listener to tell the activity to
> display the dialog or whatever.
>
> -- If no, the service starts the activity with an extra indicating "hey,
> please go ahead and pop up that dialog, please"
>
> Bear in mind that popping up an activity from a service is a very bad
> practice except in unusual circumstances (alarm clock, incoming phone
> call). If the user is in the middle of doing something else, and you
> interrupt them forcibly, and they do not think your interruption is
> warranted, they will get...cranky. :-)
>
> --
> Mark Murphy (a Commons 
> Guy)http://commonsware.com|http://github.com/commonsguyhttp://commonsware.com/blog|http://twitter.com/commonsguy
>
> _Android Programming Tutorials_ Version 2.0 Available!
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to