views:

986

answers:

2

Hello all I am a new android developer and I am currently making an application to communicate with SIP. I would like to set a background task to do all the SIP related tasks and, if possible, let it begin when the phone is started. My current thoughts are creating a Service and letting it start on the device boot, the problem is that there are two kind of Services one that uses AIDL and one that is started with an Intent.

I would like to pass some arguments to my service and let it run independently of my main Activity and be able to notify my main Activity of incoming messages. Can anybody tell me how to do that and point me to any good tutorial where I can understand the difference between the two Services? I have read a book about and some tutorials including Android Developers, but they confused me even more.

Thanks in advanced, maxsap.

+3  A: 

the problem is that there are two kind of services one that uses AIDL and one that is started whith an intent

That is incorrect. You use Intents with both local services and remote services. You only use AIDL with remote services.

I would like to pass some arguments to my service and let it run indepedently of my main activity and be able to notify my main activity of incoming messages.

To pass "arguments" to the service, you could package those as "extras" via putExtra() on the Intent you pass to startService() or bindService().

To "notify my main activity of incoming messages", you could use broadcast Intents and a BroadcastReceiver in the activity, or you could bind the activity to the service via bindService() and use listener objects.

CommonsWare
Thanks a lot one more confusing question because it seems that everyone gives a different answer, the service runs independent from the activity even if it is a local or a remote service? in other words the service is always independent from the activity and runs as a backround thread for executing time consuming tasks? correct me if I am wrong but isn't the remote(AIDL) service code executed synchronusly?
maxsap
"the service runs independent from the activity even if it is a local or a remote service?" Yes, it is merely a question of whether or not it is in the same process. "and runs as a backround thread for executing time consuming tasks?" Only if you set up a background thread, otherwise it runs on the main application thread like all of your activities and other Android components. "correct me if I am wrong but isn't the remote(AIDL) service code executed synchronusly?" Yes.
CommonsWare
Hi Commonsware, I was wondering about your "if you set up a background thread" part... If you use AIDL you get a stub, so I was wondering how do you place this stub (which offers functionality) in a Thread ?
TiGer
@TiGer - You can have the stub start an `AsyncTask` to do the background work, or convert the API parameters into an object and pop it on a work queue. Here is an example of the latter: http://github.com/commonsguy/cw-advandroid/tree/master/AdvServices/RemoteServiceEx/
CommonsWare
+2  A: 

Use a service. You can communicate with it using procedures defined in AIDL. If it has to notify you application of something, it can send your application an intent.

If you want it to have certain parameters at start up, I would store them using the preference manager.

I wrote something like this for my RandyVideos application (availiable from MiKandi for free).

I have a service in the background doing the network stuff. And the main Activity communicates with it via methods that are described in AIDL. If the service wants to talk to the Activity, it sends an intent.

Randy
thanks a lot for your help. regards maxsap
maxsap