Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

6. The Way of the Service > What Is a Service? - Pg. 148

WHAT IS A SERVICE? A Service is, at its most basic level, a class with a simple runtime lifecycle and no access to the screen. You had some contact with the IntentService back in Chapter 4 when I showed you how to retrieve an image with it, but I now have the chance to help you really dig into this simple yet powerful component. Keep in mind that while the service might be important to you, it is not more important to Android than the smooth running of the overall device. This means that at any point the system may shut down your service if it determines that it's been running too long, that it's been consuming too many resources, or that it's the third Friday of the month and there's a full moon. There is a way to tell the system not to kill you off, and I'll show what that looks like in just a second. THE SERVICE LIFECYCLE A service is, essentially, a singleton. Any component in your application may call startService with an intent that specifies the service they want to get running. If the service isn't running, Android will initialize a new one; otherwise, it will just