1. Service
A service is an application component which runs in the background without direct interaction with the user interface. As the service has no user interface, another application component can head start a service and it will continue to run in the background no matter what even if the user jumps to another application.In easy word you can simultaneously perform multiple task hence and forth like playing music, accessing web, I/O operation and many more stuff as if component bind themselves to services performing Inter Process Communication( IPC) .
Basically android services comes with 2 forms:-
a) Started Service- when any component calls startService() it activates the "started". Once started, a service can run in the background indefinitely, even if the component that has started the activity is destroyed.For example download a file , or upload a file over a network.
b) Bound Service- service is "bound" when an application component binds to it by calling bindService(). A bound service offers a client-server interface that allows components to interact with the service, send requests, get results, and even do so across processes with interprocess communication (IPC). A bound service runs only as long as another application component is bound to it. Multiple components can bind to the service at once, but when all of them unbind, the service is destroyed.
2. Why service is a background process?
Service refers to a background process that supports a full GUI application in some way, for example by registering a global hotkey or by performing network communication.
Service always runs in the main thread as the process of the main application.
3.Package Description
Service extends ContextWrapper class implements ComponentCallBack2.
4. Myth about Service
- A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of.
- A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
5. What Service Actually do?
- A facility for the application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). This corresponds to calls to context.startService(), which ask the system to schedule work for the service, to be run until the service or someone else explicitly stop it.
- A facility for an application to expose some of its functionality to other applications. This corresponds to calls to context.bindService(), which allows a long-standing connection to be made to the service in order to interact with it.
6. Life cycle of Service
Service life cycle is different from the life cycle of activity as activity is visible to user and service always runs in background.

Methods of Service life cycles
There are two reasons that a service can be run by the system.
Contants of Service :
intent,because the service had previously returned START_REDELIVER_INTENT but had been killed
before callingstopSelf(int) for that Intent.
got to or returned from onStartCommand(Intent, int, int).
(after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it,
then take the service out of the started state and don't recreate until a future explicit call toContext.startService(Intent).
(after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last
delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).
(after returning from onStartCommand(Intent, int, int)),then leave it in the started state but don't retain this
delivered intent.
START_STICKY_COMPATIBILITY
Constant to return from onStartCommand(Intent, int, int): compatibility version of START_STICKY that does
not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.
Methods of Android service
Service life cycle is different from the life cycle of activity as activity is visible to user and service always runs in background.

Methods of Service life cycles
There are two reasons that a service can be run by the system.
- If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed)
- Then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called.
Contants of Service :
- START_CONTINUATION_MASK
- START_FLAG_REDELIVERY
intent,because the service had previously returned START_REDELIVER_INTENT but had been killed
before callingstopSelf(int) for that Intent.
- START_FLAG_RETRY
got to or returned from onStartCommand(Intent, int, int).
- START_NOT_STICKY
(after returning from onStartCommand(Intent, int, int)), and there are no new start intents to deliver to it,
then take the service out of the started state and don't recreate until a future explicit call toContext.startService(Intent).
- START_REDELIVER_INTENT
(after returning from onStartCommand(Intent, int, int)), then it will be scheduled for a restart and the last
delivered Intent re-delivered to it again via onStartCommand(Intent, int, int).
- START_STICKY
(after returning from onStartCommand(Intent, int, int)),then leave it in the started state but don't retain this
delivered intent.
START_STICKY_COMPATIBILITY
Constant to return from onStartCommand(Intent, int, int): compatibility version of START_STICKY that does
not guarantee that onStartCommand(Intent, int, int) will be called again after being killed.
Methods of Android service
1. getApplication() method return the application that own this service .
2.public abstract IBinder onBind(Intent intent) method Returns the communication channel to the service. May return null if clients can not bind to the service.
Parametersintent The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Returns
Return an IBinder through which clients can call on to the service.
3. public voidonConfigurationChanged(Configuration newConfig) method has been called, your Resources object will have been updated to return resource values matching the new configuration.
Parameters
newConfig The new device configuration.
4.public void onCreate() method called when system is first created . Do not call this method directly.
5.public void onDestroy() method notify the android system that service is no longer used and is being removed.
6.public void onLowMemory() this is called when overall system is running low on memory , and actively running process should trim their memory usage.
7.public void onRebind(Intent intent) this method called when new clients connected to that service after it had previously notified that all had disconnected in its onUnbind(Intent).
intent The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
8.public void onStart(Intent intent , int startId) this method was deprecated in API level 5 instead of using this method we use onStartCommand(Intent, int, int)
.
9.public void onStartCommand(Intent intent ,int flags ,int startId) method call when every time the client want to explicitly start the service.
Parameters
intent The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
flags Additional data about this start request. Currently either START_FLAG_REDELIVERY, or START_FLAG_RETRY.
startId A unique integer representing this specific request to start. Use with stopSelfResult(int).
Returns
The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with theSTART_CONTINUATION_MASK bits.
Returns
The return value indicates what semantics the system should use for the service's current started state. It may be one of the constants associated with theSTART_CONTINUATION_MASK bits.
10.public void onTaskRemoved(Intent rootIntent) method call when service is currently running and the user has removed a task that come's from the service application.
Parameters
rootIntent The original root Intent that was used to launch the task that is being removed.
11.public void onTrimMemory(int level) called when operating system determines it is a good time to trim unused memory from is processes.
Parameterslevel The context of the trim, giving a hint of the amount of trimming the application may like to perform. May be TRIM_MEMORY_COMPLETE,TRIM_MEMORY_MODERATE, TRIM_MEMORY_BACKGROUND, TRIM_MEMORY_UI_HIDDEN, TRIM_MEMORY_RUNNING_CRITICAL, TRIM_MEMORY_RUNNING_LOW, orTRIM_MEMORY_RUNNING_MODERATE.
12.public void onUnbind(Intent intent) called when client disconnected from a particular interface from the service.
intent The Intent that was used to bind to this service, as given to Context.bindService. Note that any extras that were included with the Intent at that point will not be seen here.
Returns
Return true if you would like to have the service's onRebind(Intent) method later called when new clients bind to it.
13.public final void startForeground(int id , Notification notification) called when user want to make this service run in foreground , by giving the ongoing notification to the user.
Parameters
id The identifier for this notification as per NotificationManager.notify(int, Notification); must not be 0.
notification The Notification to be displayed.
14.public final void stopForeground(boolean removeNotification) called to remove service from the foreground and killed if more memory is needed.
Parameters
removeNotification If true, the notification previously provided to startForeground(int, Notification) will be removed. Otherwise it will remain until a later call removes it (or the service is destroyed).
15.public final void stopSelf() called to stop the service if it was previously started.
16..public final void stopSelf(int startId) depricated method and old version of stopSelfResult(int startId).
17.public final boolean stopSelfResult(int startId) stop the most recent service as per it startId time.
startId The most recent start identifier received in onStart(Intent, int).
Returns
Returns true if the startId matches the last start request and the service will be stopped, else false.
18.protected void dump(FileDescriptor fd ,PrintWriter writer, String arg[]) called to print the service state into the given stream.
Parameters
fd The raw file descriptor that the dump is being sent to.
writer The PrintWriter to which you should dump your state. This will be closed for you after you return.
args additional arguments to the dump request.
Platform service and custom services
The Android platform provides and runs predefined system services and every Android application can use them, given the right permissions. These system services are usually exposed via a specific Manager class. Access to them can be gained via the getSystemService() method. The Context class defines several constants for accessing these services.
An Android application can, in addition to consuming the existing Android platform services, define and use new services. Defining your custom services allows you to design responsive applications. You can fetch the application data via it and once the application is started by the user, it can present fresh data to the user.
Usage of Service (Implementaion and Declaration)


0 comments:
Post a Comment