Android service is one of the four main application components, Activity, Service, Brodcast Receiver and Conten Provider.
A Service is a component that runs in the background to perform long-running operations. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. It can be used as Local and Global service.

A Service is implemented as a subclass of Service:

Here is my example of usage of simple Service with some toast messages and text view setting strings.

NOTE: Services are used to work in the background of the application so the user is not interacting with the services, and does not have to. In my example i have some messages shown just to show you whats going on in the background.

NOTE: Im using Butterknife library for views binding just to save time binding views in the activity, and also save code. Here is the link for the library:
ButterKnife library

Lets start:

First of all we start from the MyService which needs to extends from Service class. When it is extended, we are obliged to overwrite some methods, and every one of them will be explained part by part.

onBind() method return the communication channel to the service. May return null if clients can not bind to the service. The returned IBinder is usually for a complex interface. You must always implement this method, but if you don’t want to allow binding, then you should return null

onCreate() method is where the service is created

onStartCommand(Intent intent, int flags, int startId) Service is started here. The service is starting, due to a call to startService(), similar as startActivity(). This method is called with the Intent object passed to startService() (start service will be explained later).
Intent – Data to be used by the Service for any sort of asynchronous execution. For example you can pass an image URL that must be downloaded.
Flag – Representing the history of this start request. START_STICKY – Service will be restarted if it gets terminated whether any requests are pending or not. The Intent of the earlier request passed (before termination) will not be resubmitted, instead null will be passed as Intent data. Hence use this option for Services that do not depend on Intent data to manage its state. All the pending requests are delivered with the START_FLAG_RETRY as the second argument (can also get using Intent.getFlags()).
START_NOT_STICKY – Service is only restarted for pending requests. The Intent data specified while making the startService() calls will be passed.
START_REDELIVER_INTENT – Similar to START_STICKY and will receive both pending and started requests. The pending requests are delivered with the START_FLAG_RETRY flag set in the second argument whereas previously started requests are redelivered with the START_FLAG_REDELIVERY flag set. Also the original Intent is re-delivered.
Pending service requests are the startService() calls since the service termination. If a Service is terminated while the onStartCommand() method is running, it is considered to be a pending request. Start requests are when onStartCommand() has been entirely executed and returned from. When a new request is submitted, i.e., the Service is started with startService(), the second flag argument passed is the value 0..
Start ID – A unique ID provided by the runtime for this start request. If the process is terminated and then at a later stage restarted then onStartCommand() will be called with the same start ID. It can be used with stopSelfResult() which is similar to stopService() but this way you can avoid stopping the Service for old start requests that have not been handled by onStartCommand() yet because maybe during that start request the Service terminated and restarted.

onDestroy() method. The system calls this method when the service is no longer used and is being destroyed. Your service should implement this to clean up any resources such as threads, registered listeners, receivers, etc. And stopSelf() is calling onDestroy() from anywhere

Here is the for this service:

onStartService() method is button for starting the service, and onStopService() method is button for stopping the service.
Service is started similar as an activity with startService(intent) method. First we declare and initiate Intent object serviceIntent with parameters this, and designated class, then we pass our created intent serviceIntent to the startService method. Same goes for stopping the service with stopService() method passing stopService intent object as parameter.

Here are the XML codes for the layouts:

The background for the buttons:

The shape for the buttons:

and logo (image), just for fun.


Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *