Android Programming – Develop an Android Service

We have discussed how to add a Service to an Android Application through the Article “Android Programming – Add a Service to the Application“. The Service what we have developed was NOT an Android Service; it was a simple Java class to provide a random message when requested.

In this Article, we will go through the steps to convert our Service class to an Android Service.

Create an Android Service

Step 1. Open the Project (the one we have developed in the above mentioned Article) in Android Studio.

What we do through this Article is, convert our Service class to Android Service class.

Step 2. An Android Service class must be derived from the base class Service. So, we will modify our MyService class to extend from Service class. The declaration looks like below:

public class MyService extends Service

Step 3. In order to use the Service class and it’s methods, we must import below packages into our Program.

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;

Step 4. As I mentioned in my previous Article, the Services and Activities we are going to use in our Android Project should be added into the Android Manifest file. Add below <service> tag details into “AndroidManifest.xml” file in the <application> tag.

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

This instructs to the Android system that we are using the MyService Android Service in our Android Project.

Step 5. Now we need to implement the Android Service class methods. What methods we need to implement? I have explained the Android Service life cycle and I recommend you go through this to understand what methods we need to implement to develop an Android Service.

We can develop Bounded and Unbounded Android Services. In this Article, we will focus on developing an Unbounded Android Service.

As mentioned in the Android Service life cycle, implement the Service life cycle methods in our MyService class. As we are developing Unbounded Service; we have to implement onCreate, onStartCommand and onDestroy methods.

Step 6. Android will call onStartCommand service life cycle method when the Service is started. And it will call onDestroy method when the Service is going to destroyed.

Just we will add a simple statement to display a message, using Toast class, whenever the Service is started and going to destroy. And return the value “START_STICKY” from onStartCommand as we are going to explicitly start and stop the Service. I will explain more on this in another Article.

If any initialization code; will go into onCreate method. We do not have any such initialization code; so we will keep it empty.

Step 7. Even though we are developing Unbounded Service, we must have to implement onBind method. Otherwise, we get below Error when we build our Program.

error: MyService is not abstract and does not override abstract method onBind(Intent) in Service

onBind method should return a bounded interface. Just return null as we are NOT developing a Bounded Service.

Step 8. Now we have the Android Service is ready. What next? We must have to start and stop the Service from our MainActivity class.

We will call startService method to start the Service and stopService method to stop the Service. We will place these methods in the Button event handler. What we do is, when the User pressed the button; we will start the Service. Again if the User pressed the button; we will stop the Service.

Step 9. Here is the complete code of MainActivity class.

Step 10. And below code is for MyService class.

Step 11. Now Build and Run the Project. Observe that, in the Emulator window our Project is displayed HELLO! button; and once you click on it; it will start the Service. And also you will see “onStart Command – Service Started” messages appeared on the Screen.

To stop the Service, click on the HELLO! button; and you will see “onDestroy – Service Stopped” message.

This proves that, our Android Service is working as expected.

You might have noticed that, we have a method getMessage in our MyService class. Why didn’t we call this method? And also where we have created our MyService object?

Yes, we didn’t create MyService object directly in our Program. But we have used Android life cycle to activate the Service. As we have created the Unbounded Service; we do not have any interface to directly call the methods from MyService class. We will discuss more on this in another Article.

We discuss more on Android Programming through upcoming Articles.

[..] David

One Comment

Add a Comment

Your email address will not be published.