DevNote: Android Service
DevNote: Android Service | ||
Technology | ||
Operating System(s):
Android
Technology(ies):
Android Service
Use Case(s):
Software Development |
||
Summary | ||
Overview
Services are Android application components representing either an application's desire to perform longer-running operations while not interacting with the user or to supply functionality for other applications to use. Each service class must have a corresponding <service> declaration in its package's AndroidManifest.xml file. Services can be started with Context.startService() and Context.bindService().
Note that services, like other application objects, run in the main thread of their hosting process. This means that, if your service is going to do any CPU intensive (such as MP3 playback) or blocking (such as networking) operations, it should spawn its own thread in which to do that work. More information on this can be found in Processes and Threads. The IntentService class is available as a standard implementation of Service that has its own thread where it schedules its work to be done.
Service Lifecycle No matter how many times a service is started, it will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed. For started services, there are two additional major modes of operation they can decide to run in, depending on the value they return from onStartCommand();
Clients can also use Context.bindService() to obtain a persistent connection to a service. This will also creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The OS will keep a service running as long as it is started, or there are connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations holds true, the service's onDestroy() method is called and the service is effectively terminated. There is some confusion surrounding the Service class and what it is not:
A Service itself is actually very simple, providing two main features:
When a Service component is actually created all that the system actually does is instantiate the component and call its onCreate() method and any other appropriate callbacks on the main thread. It is the responsibility of the Service class to implement the appropriate behavior; such as creating a secondary thread in which it does its work.
Android Developers | Service |
||
Description | ||
Code It is possible to run a service always. To accomplish this:
The service may be killed by the OS on occasion but it will be restarted automatically because the START_STICKY constant was returned from onStartCommand(). The START_STICKY constant tells the OS that if the service process is killed while it is running, then leave it in the started state but don't retain the delivered intent.
|
||
Notes / Considerations | ||
. |
Comments
Post a Comment