views:

575

answers:

3

Hi everyone,

I'm working on establishing a two-way communication between an Activity and a Service which runs in a different process.

Querying the process from the Activity is no big deal. But I want the process to notify the Activity on events. The idea behind it is this: the service runs independently from the actual app. It queries a webserver periodically. If a new task is found on the webserver the process should notify the activity.

I found this thread over at AndDev.org but it doesn't seem to work for me. I've been messing around with BroadcastReceiver. I've implemented an interface which should notify the Activity but the problem is that the listener is always null since the Broadcast from the process is done via Intent, hence the class that extends BroadcastReceiver will be newly instantiated.

How can I establish a 2-way communication? This has to be possible. Thanks for any help,

steff

+1  A: 

I think you should have the BroadcastReceiver start your activity again with the result in the Intent.

Or you could use AIDL about AIDL. The samples also have an (multiple?) example how to use AIDL and services. But AIDL might be to much of a hassle for your purpose.

MrSnowflake
...Start my Activity AGAIN? This is what I am trying to avoid...AIDL is really a hassle but seems like the way to go. I am now trying to understand RemoteServiceBinding from the ApiDemos. Tough one.
steff
A: 

You have to use BroadcastReceiver to receive intents, and when you want to communicate simply make an Intent with appropriate values.

This way you should be able to make a 2-way communication between any component.

LucaB
+1  A: 

Either use BroadcastReceiver or have the Activity register a callback or listener object that the Service calls on key events. The links above are to book example projects demonstrating each of those techniques.

CommonsWare
Can I use a BroadcastReceiver inside my Activity or do I have to create a different class? Do I have to specify the class extending BroadcastReceiver for the intent?
steff
`BroadcastReceiver` is a class -- a class cannot both be an `Activity` and a `BroadcastReceiver`. You can use an inner class of your `Activity` for the `BroadcastReceiver`, as is demonstrated in the sample code linked to above. You can use component names for your broadcast `Intents`, but this requires a public `BroadcastReceiver` class. Or you can use custom action strings, as is demonstrated in the sample code linked to above.
CommonsWare
Thanks, but I am not really sure if a BroadcastReceiver is well suited since what I want is an as-long-as-possible running process which is independent from the actual application. It should be able to show a notification in the status bar just like a new email does, even if the actual application is not running. So this means I have to start a service via startService(intent) instead of bindService(intent, connection, flags), right? I've been trying to understand the ApiDemos 'RemoteServiceClient' but pathetically failed. The more I look at 'examples' the more confused I get...
steff
"I want is an as-long-as-possible running process which is independent from the actual application" Hopefully, you don't, because that is bad design. "It should be able to show a notification in the status bar just like a new email does, even if the actual application is not running." Notifications specifically do NOT need something to be running.
CommonsWare
Ok, I think I'm just not able to express myself properly. The background process should periodically (e.g. every 5 minutes) query a webserver via HttpRequest. If/when it finds a new task it should display a notification in the StatusBar and notify the GUI (if the GUI is running) with (a reference to) the new task.
steff
"The background process should periodically (e.g. every 5 minutes) query a webserver via HttpRequest." Via `AlarmManager`, I hope. "If/when it finds a new task it should display a notification in the StatusBar and notify the GUI (if the GUI is running) with (a reference to) the new task." You can do that with an `IntentService`, `AlarmManager`, `Notification`, and a broadcast `Intent` (the latter of which should be picked up by the `Activity` if it is running).
CommonsWare
alright, sounds like a plan. I will look into this.Thanks for your help.Btw: your 'advertisement' to your github repo worked for me; I'm just buying the online version ;)
steff
"Thanks for your help." You're welcome. "I'm just buying the online version" Thanks!
CommonsWare