10

I have an Android application that has a need to perform work in the background and on a separate thread. For my first proof-of-concept I subclassed the Application class and inside onCreate() I spawn a Thread that does the background work. This works just great. However, I just realized that in the past I've used a service for situations like this.

The question is, is there a reason to do work on a Thread spawned from a Service instead of a Thread spawned by Application.onCreate()? The Service is supposed to perform "background" work (it uses the UI thread unless a Thread is used, I know) that is independent of the Activity and can run while no Activity is visible. Using an Application-based thread seems to accomplish all this just as well. By not using a Service it actually removes complexity because the Activity just accesses the Application singleton. As far as I know I have no need to bind to the Service.

Will I get bit by lifecycle corner cases that using a Service would prevent? That's the only concern I have over this approach, but otherwise I'm not sold on the benefits of a Service.

Mark Herscher
  • 1,761
  • 2
  • 19
  • 32

2 Answers2

11

The difference would be if you want the thread to run in the background only when the Activity is running or if you want it to continue to run when the user leaves.

Services are capable of running in the background even when the Activity is no longer available. They are intended to be used when your app should continue to do work without any user involvement in the near future. If you run the Thread in the Service, the thread will continue to run even when the user leaves the app. This can be beneficial sometimes as the user may want you to keep downloading a really large file but doesn't want the app to continue to run in the foreground. Then, a few hours (days, months, years) later the user can re-enter the app to read the file.

If, however, you're using a thread that needs to constantly update the UI based on results, it may be more beneficial to launch it within the Activity since it has no real purpose to run in a Service. It also may be easier in your program for your Thread to talk to the UI if it's in the Activity rather than the Service. (There may be some performance benefits as Android doesn't have to handle yet another Service on it's list, but that's purely speculation on my part. I have no proof of it.)

NOTE: Threads created in Activities will still continue to run even when the Activity quits. However, this is merely because the app is still in memory. The Activity and it's thread are on a higher priority to be deleted from memory than a Service thread when the Activity is no longer within view.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • 1
    Right, I know the Activity lifecycle. The question was, what do I lose by not using a Service and just running the thread by itself? I'm spawning the thread in the Application, NOT the Activity (not that it matters for the Thread's lifecycle, as you noted). I know that services are less likely to be killed by the system, but even if my activity gets destroyed that does not guarantee the application will be. An application can exist with no activities. – Mark Herscher Oct 28 '11 at 21:55
  • Oh wait I see. In that case, I would think you would lose the ability to bind to the Service from multiple applications if the Thread you had needed to be shared. Also, I think the Application as a whole would still be subject to deletion if there are no services running on it, so once the user leaves the last application on the stack, the thread is subject to removal just like it would if it was spawned in the first Activity. – DeeV Oct 28 '11 at 22:01
4

If your application is not either in the foreground, or visible, then it's more likely to be killed off by the system. If you run your code as a service, rather than a thread spawned by a background process, then your task will survive for longer. No guarantees, so you still need to manage the process lifecycle properly, but running as a service is likely to give more reliable results.

See http://developer.android.com/guide/topics/fundamentals/processes-and-threads.html

Teasel
  • 960
  • 6
  • 6
  • So if I don't care if the application survives when all activites are hidden, what I'm doing should be fine? – Mark Herscher Oct 28 '11 at 22:05
  • Errr, yes! If you don't really mind whether your code runs or not, you can spawn it from within an activity thread and it will be the first task to be killed off. But if you want your thread to stay around for as long as possible, you should spawn it from within a service thread. – Teasel Nov 01 '11 at 23:00