Home > Android > Android Service

Android Service

* A Service is an application which has the ability to run in the background without displaying any user interface.
* A Service is a class which derives from android.app.Service.
* All Services must be declared in the manifest file.
* Services run without a dedicated GUI, but, like Activities and Broadcast Receivers, they still execute in the main thread of the application’s process.
* A Service could be, facility for an application to expose some of its functionality to other applications.
* Started Services receive higher priority than inactive or invisible Activities.
* Android implements several Services including the Location Manager, Media Controller, and the Notification Manager.

What a Service is not?

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).


Global access to a service can be enforced when it is declared in its manifest’s <service/> tag.
By doing so, other applications will need to declare a corresponding <uses-permission/> element in their own manifest to be able to start, stop, or bind to the service.

Creating Service

* To defi ne a Service, create a new class that extends the Service base class.
* Override onBind and onCreate methods.

android services

* We may also override onStart which is called whenever the Service is started with a call to startService.
* Once constructed, a new Service should be registered in the application manifest.

Starting, Controlling, and Interacting with a Service

To start a Service, call startService; you can either implicitly specify a Service to start using an action against which the Service is registered, or you can explicitly specify the Service using its class.

// Implicitly start a Service
      startService(new Intent(MyService.MY_ACTION));
      // Explicitly start a Service
      startService(new Intent(this, MyService.class));

Stopping Service

To stop a Service, use stopService, passing an Intent that defi nes the Service to stop.
If startService is called on a Service that’s already running, the Service’s onStart method will be executed again.
Calls to startService do not nest, so a single call to stopService will terminate it no matter how many times startService has been called.

ComponentName service = startService(new Intent(this, BaseballWatch.class));
      // Stop a service using the service name.
      stopService(new Intent(this, service.getClass()));
      // Stop a service explicitly.
      try {
      Class serviceClass = Class.forName(service.getClassName());
      stopService(new Intent(this, serviceClass));

Public methods of android.app.service

* Application getApplication();
* abstract IBinder onBind(Intent intent);
* void onConfigurationChanged(Configuration newConfig);
* void onCreate();
* void onDestroy();
* void onLowMemory();
* void onRebind(Intent intent);
* void onStart(Intent intent, int startId);
* boolean onUnbind(Intent intent);
* final void setForeground(boolean isForeground);
* final void stopSelf();

Services in Android

Android has two types of Services
* Local services : e.g. Email app. Local services are services that are called only by the application that hosts them.
* Remote services : e.g. Router app. Remote services are services that support a Remote Procedure Call (RPC) mechanism.