Easy Tutorial
❮ Intelligent Dev Ops Docker Use Container Create Image ❯

Android Service Tutorial

Category Programming Techniques

In this article, we discuss an important component in Android application development—the Android Service. Unlike Activities, Services run in the background in Android, have no user interface, and have a different lifecycle from Activities. Services can be used to implement background operations, such as loading a webpage from a remote server. We can use Services to achieve multitasking in Android.

Overview of Android Services

We know that if system resources become scarce, Android Activities can be started, stopped, destroyed, and may even be recreated. Services are designed to have a longer lifecycle. Services in Android can be started from Activities, broadcast receivers (Broadcast receiver), or by other Services.

It must be noted that using a Service does not automatically create a new thread. Therefore, if we want to implement a simple logic in a Service that does not require long processing time, we do not need to run it in a separate thread. However, if we need to implement complex logic that will take a long time to process, we must be careful when creating a new thread, otherwise, due to the Service running on the main thread, it may cause ANR issues (Application Not Responding).

The main scenarios for using Services in Android are as follows:

A typical example of the first scenario is when an application needs to download data from a remote server. In this case, you can use an Activity that interacts with the user, and start the Service while the user is using the application to complete the work in the background. Another scenario is when the Service has completed its task and sends a message to the user.

In the second scenario, we want to "share" some common functionalities so that different applications can reuse them. For example, we can assume that we have a Service that can send emails, and we want to share this service among several applications, so we don't have to rewrite the same code. In this case, we can use IPC so that the Service exposes a "remote" interface that can be called by other applications.

Service Basics

Now that we have a better understanding of Services, let's create one. To create a Service in Android, we need to inherit from the Service class.

public class TestService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {        
        return null;
    }

}

As you can see, we only implement a method called onBind. In the example above, we use a local service, so the method returns null. As mentioned earlier, Services have their own lifecycle, so we can override some callback methods to handle their different states:

public class TestService extends Service {

    @Override
    public void onCreate() {        
        super.onCreate();
    }

    @Override
    public void onDestroy() {        
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {        
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {        
        return null;
    }

}

The first method, onCreate, is only called when the Service is created. If the Service is already running, this method will not be called. We cannot call it directly; it is called by the system.

The onStartCommand method is the most important method because it is called when we need to start the Service. In this method, we have the Intent that is passed in when running the Service, so we can exchange some information with the Service. In this method, we implement our own logic: if it is not a time-consuming operation, it can be executed directly in this method, otherwise, a thread can be created. As you can see, this method needs to return an integer value. This integer represents how the system should handle this Service:

OnDestroy is the method called by the system when the Service is about to be destroyed.

Once you have a custom Service class, you need to declare it in the Manifest.xml so that you can use it.

<service android:name=".TestService"
         android:enabled="true"/>

Starting and Stopping Services

As we know, a Service is started and eventually stopped to complete its task. Suppose we start it from an Activity and can pass some information to the

❮ Intelligent Dev Ops Docker Use Container Create Image ❯