views:

180

answers:

1

I currently have a Service and an Activity in my application.

I currently bind the Service to the activity without using AIDL as the Service and the Activity are in the same application.

This allows me to call the methods from the Service within my Activity when I require them, however it doesn't let me call the methods of the Activity from within my Service when I want to.

Can anyone comment on what would be the best way to achieve this?

I could use Intents but is there an alternative option?

I want to have tight communication between the Service and the Activity, I want to be able to call an Activity method from my Service when an event happens.

Thanks to ognian for the excellent advice.

EDIT 2:

I now have it working the way I want, however I have come across a problem.

My service gets status updates and my Activity is then supposed to react to the update sent on from the Service.

The problem is that when I start my activity I get the Dialling Status and then Connected status before the onBind is called and I get the instance of iCallDialogActivity.

The following output from the logcat might make things clearer.

06-28 10:56:48.702: DEBUG/TestPhone(3498): Status: EStatusDialling
06-28 10:56:48.751: DEBUG/TestPhone(3498): Status: EStatusConnected
06-28 10:56:49.122: DEBUG/TestPhone(3498): Status: onBind Called    <------------- 
06-28 10:56:49.141: DEBUG/TestPhone(3498): Status: iCallDialogActivity instance  <------------- 
06-28 10:56:51.641: DEBUG/TestPhone(3498): Status: EStatusDisconnecting
06-28 10:56:51.651: DEBUG/TestPhone(3498): Status: EStatusIdle

I need to be able to use my iCallDialogActivity when I get the Dialling and Connected Status notifications.

But this gives me a NullPointer Exception due to it not being created in time when my Activity starts, binding is the first thing I do in my Activities onCreate().

Is there a way to make it bind straight away?

+1  A: 

Hi,

You can't call directly methods on your Activity's instance, you must use the same IPC mechanism you're calling Service methods trough AIDL.

First, you need to declare some methods you wish to expose as callbacks. Do that in a separate .aidl file. Then add a setter for that callback to the Service's AIDL, for example:

void setMyCallback(inout IMyCallback myCallback);

if you made IMyCallback.aidl. Remember to import it in Service's .aidl, even if it's in the same package. In your Activity, instantiate a Stub from that .aidl, and pass it to the Service before calling back.

ognian
So I need to go the AIDL route despite not requiring Inter Process Communication?
Donal Rafferty
Yes, that's how Android is designed. If you app doesn't span across processes, you can have some static instance getter in your Activity and use it directly. I'll work, but it's not considered best practice. Android's IPC is very lightweight to worry about performance in normal scenarios.
ognian
Can you point me to a nice tutorial on it? I cant seem to find one where the AIDL is used to allow a Service call methods from an Activity.
Donal Rafferty
Also, I currently dont have a Service.aidl file, my service is currently binded to the Activity without using the aidl
Donal Rafferty
I don't know about tutorial, but in Android's documentation there is a sample .aidl that has a method with argument of another .aidl: http://developer.android.com/guide/developing/tools/aidl.html#aidlsyntax
ognian
@ ognian, Thanks for your help so far, I have edited my question with what I have done so far and think I'm nearly there, However I dont understand how to link the methods in my Activity aidl and my Activity and then call them in my Service?
Donal Rafferty