tags:

views:

216

answers:

4

Is there a significant difference in time needed for sending data over a service or by using an intent?

Are there general advices when to use service and when to use intents?

+1  A: 

Many methods to pass data between activities. See here for tips on a way to choose.

gatnowurry
A: 

It depends of what you need.

Intent is preferable if you can. You will be able to send primitives from an activity to an other, and using startActivityForResult() you'll get an intent back to the caller Activity.

Service is for data processing in the background and can be very CPU/Memory consuming. With a Service, you have to create an interface between your Activity and the Service, so you can call basic methods of the Service directly from the Activity, you can control the service from the Activity.

This is really not the same purpose. Read documentation about Intents and the information you can Bundle in it, that's probably what you need.

Rorist
thanks for your answer. i forgot to mention that my activities are distributed over two differen applications (projects in eclipse).
Roflcoptr
A: 

When you want to pass data from your current activity to a new activity, the best is to pass a Bundle along with your Intent. It is used to pass on "acquired" user data.

Services run in the background while another activity is still in the foreground. "Background" doesn't mean that it doesn't display - most services have a graphic visualisation of some sort - it means that it isn't part of the activities stack. For example, your Activity may be sending a text message and your Service may be a soft keyboard. Services can communicate with activities - in this instance, your keyboard of course needs to send the characters to the text message Activity - but it often involves using a rather complex interface. It is used for collecting and passing on "live" user data to an Activity.

FreewheelNat
thanks for your answer. i forgot to mention that my activities are distributed over two differen applications (projects in eclipse).
Roflcoptr
+1  A: 

These are two completely different things. The question isn't which is faster, but what you are trying to do.

If you want to transfer data from one activity to another, you pass it through the intent. If this is not sufficient for you (too much data for example), you can take other approaches but they will not involve a Service. For example, you may have a singleton holding your shared data, which both activities access... but be extremely careful about your process being killed at various points which causes the singleton to go away (and using a Service for this won't let you get away with not dealing with such a situation).

A Service is to do some work in the background even if the user isn't directly interacting with the app. Especially if we are talking about stuff within one .apk (and thus typically one process), there are very few other reasons to use a Service.

hackbod