Friday, June 19, 2015

Service V/s Thread

Service V/s Thread :


A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.


If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or Handler Thread, instead of the traditional Thread class.


Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

1 comment:

  1. Lucky Club Casino Site Review 2021 | Lucky Club Casino
    Lucky Club Casino Review 2021. Play Slots, Live Dealer, Roulette & More. Sign Up Bonus up for one of the most trusted and 카지노사이트luckclub trusted online casinos in the world.

    ReplyDelete