Android Programming

Android Service

A service is an android component used to implement long running operations.

When should I use a Service?

Use a service if you have a long running operation or a task that has a long lifecycle.

An example is a background data synchronization task.

In that type of task you can create a service that runs periodically or as needed. You can make use of a system alarm.

Then your service can terminate when the task is over.

How do I use a Service

  1. Well, a Service is just a class that you subclass.
    public class MyService extends Service {|
  2. Being a class you can optionally give it a constructor:
    public MyService() {}
  3. Then you override the onBind() callback:
     public IBinder onBind(Intent intent) {
         // TODO: Return the communication channel to the service.
         throw new UnsupportedOperationException("Not yet implemented");