CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Android Programming – Add a Service to the Application

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

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

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

What we need to 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, World!” 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 the Java Class menu item to add a Java Class to the Project. Android Studio will open the “Create New Class” dialog to allow us to add the class.

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

Create New Class dialog
Create a New Class dialog

Once the changes are done, click on the 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 the “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 a Random class. You have to take care in your code to NOT exceed the max index of the messages array when you access the message from it.

We use the 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 a 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 the 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 the “Hello!” button. You must refer to the previous 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 a 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 for 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

Android Programming – Add a Service to the Application

4 thoughts on “Android Programming – Add a Service to the Application

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top