Really simple Service queries

Put your problem here if it does not fit any of the other categories.

Really simple Service queries

Postby ChrisFlynn » Thu Nov 26, 2009 12:54 pm

What is the difference between local and remote services? My understanding is that remote will continue even after the invoking activity(?) is destroyed (so they're independent). But why would I ever use a local service instead of a Thread/Runnable?


Is reading variable directly from a service allowed (remote variable call), or is a getter method absolutely required?


Can anyone recommend any ridiculously simple examples of a remote service - one binder, which connects ('binds'?) to an existing service or creates if one's not already started?
All examples are either local services (MusicDroid), have multiple binders (Google), or are unclear...


Many thanks!
ChrisFlynn
Freshman
Freshman
 
Posts: 3
Joined: Thu Nov 26, 2009 9:41 am

Top

Postby padde » Thu Nov 26, 2009 3:53 pm

I cant find the site again where i find once that easy example.. so i just
attach the code :)
Attachments
RemoteService.zip
(9.42 KiB) Downloaded 101 times
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby ChrisFlynn » Thu Nov 26, 2009 4:04 pm

padde wrote:I cant find the site again where i find once that easy example.. so i just
attach the code :)


Looks very promising - thanks! I'll have a play this afternoon.

Can you confirm the difference between local and remote? Nowhere seems to define it!
ChrisFlynn
Freshman
Freshman
 
Posts: 3
Joined: Thu Nov 26, 2009 9:41 am

Postby padde » Thu Nov 26, 2009 4:40 pm

As far as i know remote services running in its own process. So they are independent from the activities that binds to them or starting them. A local service is running in the same process as the rest of the application and gets killed
when the app gets killed.. killed from the system i guess.. not when the user quits the app. Otherwise a local service
wouldn't really do much sense.

And about that remote variable call thing.. that istn possible. You could use broadcasting or intents with extras to communicate with the service if you really want to avoid aidl. But aidl isnt that hard and its the absolut prefered way
of IPC.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby ChrisFlynn » Thu Nov 26, 2009 5:37 pm

I thought so - thank you very much! I haven't got my head fully around lifecycles, but I think that makes sense. So on my HTC Hero, if I press 'back' then the app is still there, and the local service is still connected; until Android decides it's time to be destroyed, at which point the service goes too? So local services would be used for a long-running task which should run even when the activity isn't visible, but isn't essential that it completes?


Also, the Service class seems to have two ways to work - either startService() which assumes there's some payload code in onStart() to work through and then die; or by bindService() which assumes I'll use IPC calls via my AIDL stub, and stays around until I explicitly stop it?
ChrisFlynn
Freshman
Freshman
 
Posts: 3
Joined: Thu Nov 26, 2009 9:41 am

Postby padde » Fri Nov 27, 2009 12:46 am

Yep this is almost correct. The android system kills hole processes.. so a local service gets killed if the activity gets killed. But if you close the activity gently your local service should still run because the process is still running.
A remote process have a own process and so it doesnt care what your activities do or if the get killed.
But a remote service could get killed too from the system if your device is really running out of memmory.

The onStart and onBind methods of the service a really two lifecycles. One lifecycle in case the service is initially
started by an startService method and the other if its started via onbind with start flag.

If you start the service via onbind your service will get closed if all activities bound to it call unbind or if you call the stopself method in the service. I dont know what happens if you later call startservice and all bound activities call unbind.. but i guess the service stops because it was initially started via onbind.

However if you start the service initially with a startservice method your service runs until someone invokes stopservice or stopself in the service. Between this startservice and stopservice calls you can bind or unbind as you wish. If you close it while another activity is bound to the service i think the activity simply will get forced to unbind.
But i never tried that.. so not 100% sure.

All in all the startservice method is good to pass commands to your service and binding to it is good for long term interaction.

A few days ago i finished an app myself that heavily uses a remote service. The hole app is based on a widget
that starts/stops the remote service and you can start other activitys that are part of the app with the widget.
Those other apps again bind to the service. So the service is doing all the groundwork like location updates
and webservice connection.

In about 3 weeks i could share the code.. but maybe a bit later because without comments or any other kind of documentation its really hard to understand the interesting parts i think.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 17 guests