Service Vs Thread

by tarek.attia » Sun, 13 Jun 2010 21:46:55 GMT


Sponsored Links
 Hi all,

I have a question what to Use ,Service Or Thread,as both run in the
background

--



Service Vs Thread

by skink » Sun, 13 Jun 2010 22:47:39 GMT


 


no, services run in the UI thread

pskink

--


Sponsored Links


Service Vs Thread

by Kumar Bibek » Sun, 13 Jun 2010 22:52:24 GMT


 Services obviously don't run on the UI thread. It runs in the
background. However, Services have their own lifecycle, and can be
easily managed by applications. You can think of Services as an
extension of threads, in the sense that both of them do not run on the
UI thread.

Thanks and Regards,
Kumar Bibek






--



Service Vs Thread

by skink » Sun, 13 Jun 2010 23:21:59 GMT


 


only IntentService runs in its own thread.

ordinary Services run in the main (UI) thread of their hosting
processes:
  http://developer.android.com/intl/fr/reference/android/app/Service.html 

pskink

--



Service Vs Thread

by tarek attia » Mon, 14 Jun 2010 00:06:45 GMT


 Services Run in the background as stated here

*Services*A *service* doesn't have a visual user interface, but rather runs
in the background for an indefinite period of time
 http://developer.android.com/guide/topics/fundamentals.html 
So my question is when to use Services and When to Use threads ??...and Can
I make services inside threads??





>



Service Vs Thread

by Olivier Guilyardi » Mon, 14 Jun 2010 00:23:27 GMT


 


There is confusion here, because you are referring to two different meanings of
the word "background".

A thread, in Java but also in many other languages, allows for parallelization
within a single program/process, that is: doing several things at the same time.
If your program has a GUI and needs to do something heavy then you need to do it
in the "background" and thus within a separate thread, to avoid freezing the UI.
But this thread will terminate when your program terminates.

A service, in Android, is something which runs even when the application is not
in the foreground, that is: it continues to run when the user leaves your
activity and returns to home, or when the phone goes to sleep. If you don't need
this then you don't need a service.

As you may see, "background" here means something quite different, because a
service never runs in a dedicated /thread/. By default it runs in the main
application thread, and may optionally run in a separate /process/:
 http://developer.android.com/intl/fr/reference/android/app/Service.html 

--
  Olivier

--



Service Vs Thread

by Olivier Guilyardi » Mon, 14 Jun 2010 00:35:32 GMT


 




One thing that I forgot: if you need to have something to keep on running while
the user goes from one of your activities to the other, then you may also need a
service.

But the best would be that you explain what you are trying to do.


--
  Olivier

--



Service Vs Thread

by tarek attia » Mon, 14 Jun 2010 00:49:54 GMT


 Thanks you so much for this clarification ,,exactly I want to make an
application that keeps running while the user may went of the main
application ,,and I guess Service will be the best candidate for doing
that  ,,,,,,,

But what if the application will take much time ,,so should I make a thread
that embedded inside it a Service??








>



Service Vs Thread

by Frank Weiss » Mon, 14 Jun 2010 01:03:51 GMT


 The problem appears to mainly be the formulation of the question. In
Android, an application doesn't run - it's just a collection of
resources. Of those resources, Activities and Services can run, but
their runtimes are relatively brief. Please try to explain what your
application needs to do when it has no visible Activity. The
recommended solution would depend on that.

--



Service Vs Thread

by Olivier Guilyardi » Mon, 14 Jun 2010 06:01:11 GMT


 


Yes, exactly. If you need to do something heavy in the service, it must happen
in a dedicated thread, otherwise you will freeze the application main thread
(and thus the UI) which you should never do.

So, you might need both a service *and* a thread.

--
  Olivier


--



Service Vs Thread

by Olivier Guilyardi » Mon, 14 Jun 2010 06:09:45 GMT


 




Little clarification: of course this thread must be "embedded" in the service,
not the opposite. The service acts as an entry point, in which you can create
several threads if you like.

--
  Olivier

--



Service Vs Thread

by tarek attia » Mon, 14 Jun 2010 21:55:18 GMT


 Thank you so much for such help ,,,,

Again all my application requires the following :-


   1. The application should be able to run in the background when other
   applications are running in the foreground.
   2. When the application is on the foreground, it should shows certain
   stuff to the user

So Service is the best candidate for that ??








>



Service Vs Thread

by Kostya Vasilyev » Mon, 14 Jun 2010 22:27:32 GMT


 Yes, this is exactly what services are for.

Services are a way to tell Android that certain code in your application 
is important to the user, and Android should make an effort to keep it 
running even when it doesn't have active activities.


For user notifications, take a look at NotificationManager and its 
related class, Notification.


-- Kosyta

14.06.2010 17:54, tarek attia ?????:
Thank you so much for such help ,,,, Again all my application requires the following :- 1. The application should be able to run in the background when other applications are running in the foreground. 2. When the application is on the foreground, it should shows certain stuff to the user So Service is the best candidate for that ?? On Mon, Jun 14, 2010 at 1:09 AM, Olivier Guilyardi <l...@samalyse.com >> Thanks you so much for this clarification ,,exactly I want to make an >> application that keeps running while the user may went of the main >> application ,,and I guess Service will be the best candidate for doing >> that ,,,,,,, >> >> But what if the application will take much time ,,so should I make a >> thread that embedded inside it a Service?? > > Yes, exactly. If you need to do something heavy in the service, it must happen > in a dedicated thread, otherwise you will freeze the application main thread > (and thus the UI) which you should never do. > > So, you might need both a service *and* a thread. Little clarification: of course this thread must be "embedded" in the service, not the opposite. The service acts as an entry point, in which you can create several threads if you like. -- Olivier --



Service Vs Thread

by schwiz » Mon, 14 Jun 2010 22:41:54 GMT


 your service will want to spawn its own thread or asynctask to do your
heavy work or else you will get a forceclose.




--



Other Threads

1. Looper/Handler issue

Hi.
I'd like to know if Looper can enable handler to handle one message at
a time, instead of handling all the messages in the queue together in
one blocking call when Looper.loop() is called.

Because the looper sends all the messages in the queue to the handler
one after the other in one blocking call, my application displays the
ANR message. Instead, I want to be able to handle one single message,
one blocking call at a time - the instant it falls into the queue.

The following code illustrates my problem: (pls note the lines
referred: 1,2,3,4&5)
Question 1: After calling looper.loop in line3, line4 and 5 don't run.
Why is this? I tried quit(), but it doesn't help.
Question 2: Instead of displaying 0,1,2,3,4 in one blocking call (at
line3), I'd like to have 5 different blocking calls for each.
Essentially (sorry, if i sound repetitive), I want to be able to use
looper in such a way that each time, it ensures only one message gets
handled. Is there a way to do this?

I'd appreciate any thoughts on this.
Thanks for the help!!

public class TestLoop extends Activity implements Runnable {
        private Thread bkdg;

        private Handler handler = new Handler() {

                @Override
                public void handleMessage(Message msg) {
1)                      Log.i("Bkdg thread", "Message what = " + msg.what);
                        super.handleMessage(msg);
                }

        };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        bkdg = new Thread(this);
        bkdg.start();

    }

        public void run() {
                Looper.prepare();
                int i = 0;
                while(i < 5) {
2)                      handler.sendEmptyMessage(i);
                        i++;
                }
3)              Looper.loop();
4)              handler.sendEmptyMessage(5);
5)              Log.i("bkdg thread", "reached here");
        }

}
--~--~---------~--~----~------------~-------~--~----~

2. Physics engines for Android

Hi,

     I want to try any physics engine for 2D and 3D in Android. So far
I have learnt that JBullet and JBox2d can be used, and JBox2d is a
better one among them. But there in not much available documentation
available and it even uses some concepts which are not supported by
Android like Applets.Even some posts in various forums suggested that
there are still no ideal physics engines available for Android. Are
there any plans for implementing specific physics engines for Android?
Can OpenGL be used for these ?

       Help me out in deciding which physics engine is best suitable
for Android.

Thanks in advance.
--~--~---------~--~----~------------~-------~--~----~

3. Flicker with multiple frame buffer devices( Fb0 and Fb1 simultaneously )

4. Identifying phone hardware

5. how to load the related Bitmap according to input

6. Does StreetView work in Android?

7. Should I use Canvas or SurfaceView for a chess game?