Remote Service Tutorial

Tutorials with advanced 'difficulty' and more Lines of Code.

Postby DRJTower » Thu Mar 18, 2010 8:09 pm

Thanks! After spending hours looking at tutorials for this, yours was by far the easiest to follow!

I have one related question.
You say that an "object that is passed between the two needs to be marshaled"
Is there any way to pass objects between processes (using serializable perhaps?)

If I could do this, It would save me tons of time. (From writing a protocol, and then going back and changing it in both activity and service when something needs to be changed, not to mention debugging)

Thanks for help!

I think I found a way that you can send objects to/from activities to/from services
IF they are serializable ( see Intent.putExtra(String, Serializable) and Bundle.getSerializable(key) )

However whenever I try to do this with my own objects I get casting errors.

I give Intent.putExtra(String, Serializable) my object of some class
Bundle.getSerializable(key) works (or at least it does not return null)

when I try to cast the object back to my class I get casting errors
and the object is not an instance of my class

I have user serialization before with files and sockets, and never had this problem.

Has anyone tried using this?
Posts: 4
Joined: Thu Mar 18, 2010 6:35 am


Postby trraj_pap » Tue Mar 23, 2010 1:22 am

Thanks for your example, and it worked well.
I have different question on this regard.

I try to run same but from two different below

I have two projects in eclipse workspace – (So two processes)
one is activity which is UI app
Second one is “service” which exposes some apis to client (above UI App)

I want my first UI app(activity) to call some apis in second service app, and in doing so, I get the following error:
INFO/ActivityManager(61): Start proc com.example.yfservice for service com.example.yfservice/.YCIService: pid=299 uid=10034 gids={}
WARN/dalvikvm(289): VFY: unable to resolve static method 31: Lcom/example/yfservice/IYCIService$Stub;.asInterface (Landroid/os/IBinder;)Lcom/example/yfservice/IYCIService;

I am not sure if there is any sample code such scenarios (IPC between two apps) ? Can someone please point me to right reference app/code?

Once Poster
Once Poster
Posts: 1
Joined: Tue Mar 23, 2010 1:17 am

Postby jean_francois » Tue Mar 30, 2010 8:14 pm

saigeethamn wrote:
aibo99 wrote:1. User starts the RemoteServiceClient app, then clicks 'Start' button on UI to start the RemoteService thread remotely.

2. He just pushes 'Home' button on handset to leave the RemoteServiceClient app.

3. After several minutes, he starts the RemoteServiceClient app again, and clicks 'Start' button on UI to start the RemoteService.

My questions :

a) If the first instance of RemoteService still alive, will it create another instance of RemoteService in step 3 ? (how to avoid multiple instances created ?)

yes, the first remote service is still very much alive. if you come back and restart the client and click on the start button again, you will get a message that the remote service has already been started. It will not start another service unless the first one has been killed by the platform due to memory reasons. In case that has been killed, the start button will start a new service. You can make out the difference by seeing the counter. If it has been running for all the time the client moved away, when you come back and bind and invoke, it will give a large count.

I am not sure about this. I Binded, Started, Released, Closed the application, and then re-opened it and after Binding I could see the counter incrementing with Invoke, but the Service status was 'not started', and starting it again caused another instance of RemoteService to be started, thereby increasing the count by double the original speed. ... d75d?pli=1

This may have something to do with it.
Posts: 6
Joined: Fri Mar 12, 2010 1:20 am

Postby eppinator » Tue Apr 06, 2010 9:29 am

Nice Tutorial!

I extended it for my needs. From my Actitvity I add a callback to my service...

My Problem now is: how can I assure, that before my Activity is going to be destroyed (normally because the user quits it) the callback is removed from the service.
If the service is still bound, it works fine. In the onDestroy i call service.removecallback(...)
But if the service is not bound, a connection has to be established before removing the callback... but thats an asynchronous task and if I call it in onDestroy() I somehow have to wait unitl onServiceConnected is called again...
any Ideas how I can solve this?

My service can have multiple clients and stops itselft if no more callbacks are registered and no clients are bound. Unfortunately "onCallBackDied" gets only called if an activity is killed due to error, but not if its closed regulary...
Junior Developer
Junior Developer
Posts: 10
Joined: Thu Feb 11, 2010 1:00 pm

Re: Remote Service Tutorial

Postby spvakharia » Tue Jun 19, 2012 6:36 am

Hi .
Thanks for nice tutorial.
If I want to use remoteService Value to myService - I defined in RemoteService like method like >.

public int getCounter() throws RemoteException {
Random mRandomGenerator = new Random();
mcounter = mRandomGenerator.nextInt(9);

return mcounter;

But it will not work ..will you help me for the same
Once Poster
Once Poster
Posts: 1
Joined: Tue Jun 19, 2012 6:32 am


Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest