Android Programming – Add a Service to the Application

In our previous Article “Android Programming – Create an Activity and Add a Button handler“, we have discussed adding an Activity and Add a Button handler. Once the Button clicked; we have displayed “Hello, World!” message on the Activity screen.

We have used below statement to display “Hello, World!” message on the screen.

Toast.makeText(this, "Hello, World!", Toast.LENGTH_SHORT).show();

What we do now is; we will extend our Project, to Add a Service and the Service will deliver the message to display it on the screen. That means, instead of passing the “Hello, Word!” message; we will get this message from our Service and that will be displayed on the screen.

Step 1. Open our Project in Android Studio. Now we need to add a Java class which is our Service to deliver the messages to the Activity.

Step 2. Right click on the Package where you would like to add the Class; and select New -> then select Java Class menu item to add a Java Class to the Project. Android Studio will open “Create New Class” dialog to allow us to add the class.

Step 3. From “Create New Class” dialog, give the name of the class in “Name:” field (I have entered MyService as the class name) and keep the default values. Observe that, package name is already populated; because, you have selected the Package where you want to add the Java Class. Otherwise, you need to enter the package name here manually.

Create New Class dialog
Create New Class dialog

Once the changes done, click on OK button to add the Java Class to the Project.

Step 4. Notice that, Android Studio added a Java Class (in this case MyService class) to the Project and added a default class constructor without any arguments to it. The name of the file AndroidStudio generated is “MyService.java” file.

Now we will add a public method getMessage to this class; which delivers a random message. Before that, to keep the random messages; add a private member m_Messages to the class, which holds these messages, which is an array of strings.

Step 5. Inside, getMessage method we will add the code to return the random messages. For this we use Random class. You have to take care in your code to NOT to exceed the max index of the messages array when you access the message from it.

We use Random class’s nextInt function; which returns the next random integer and always should be within the given range.

Step 6. As we are using Random class; we must import java.util.Random package into our code. Putting it all together our Service class code looks like below:

package com.codesteps.david.startup;

import java.util.Random;

public class MyService {
    public MyService() {
    }

    // MyService holds these messages to deliver when requested
    //
    private final String[] m_Messages = {"Hello, World!", "Good Morning!", "Good Evening!", "Good Night to All!"};

    // Return the random message
    //
    public String getMessage() {
        Random rand = new Random();

        return m_Messages[rand.nextInt(4)];
    }
}

Step 7. Now we need to access this getMessage method from Activity class. We will instantiate our Service class and access its’ method getMessage through its’ object. We will update this in our code; where it will execute when we click on “Hello!” button. You must refer previos article, mentioned above; to understand this.

Putting all together, the method sayHello in our Activity class looks like below.

public void sayHello(View v) {
    MyService svc = new MyService();
    Toast.makeText(this, "Displaying random message: \n" + svc.getMessage(), Toast.LENGTH_SHORT).show();
}

Step 8. Build & Run the Project. In Android Emulator you will see our Activity screen displayed with a “HELLO!” button on it. Click on it and observe that; every time you clicked on it, it shows different message on the screen. That proves, our program is working fine.

Display Random Message
Display Random Message

Have we done with this? Does it so simple to create a Service? Not yet done. And it is NOT SO SIMPLE to create an Android Service. Our Service is limited to our Application. It is not possible to other Android Applications to access our Service.

Yes, it is VERY IMPORTANT to understand that, what we have developed is just a Service class, which delivers a random message; IT IS NOT AN ANDROID SERVICE.

We will extend our Service to an Android Service in our next Article.

[..] David

2 Comments

Add a Comment

Your email address will not be published.