CodeSteps

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

Android Programming – Create a background Service

Through our Previous Articles, we have discussed Creating an Android Service and Creating a Bounded Service. These services are user-interactive; which means, these are foreground services. We can interact with the Services through the User Interface elements. We have another type of Android Service, which is called Background Service.

In this article, we are going to discuss Create a background Service.

Background Services are useful to do some background operations; for example, downloading the file in the background. Usually, these Services do not have any user interactions.

IntentService class

IntentService class is used to handle asynchronous requests. We call these requests, Intents. These Intents handle through a single background thread. All these requests run sequentially. That means the requests will wait until their predecessor request completes the operation.

Create a background Service

Step 1. Create our own class MyBkgrndService, which is derived from this IntentService class. This is simple to add if we use a built-in wizard from Android Studio.

Step 1.1. Once you created the Project (with the Main Activity), right-click on the app folder (from the left side panel); and then select New -> Service -> Service (Intent Service) menu item. Android Studio will display the “New Android Component” dialog, to allow configuring the new component.

Step 1.2. Provide the name of the class in the “Class Name:” field and click on the Finish button to create the class.

Step 2. Observe that, the wizard automatically creates boilerplate code for us. Below is the overridden function required to handle the requests.

@Override
protected void onHandleIntent(Intent intent)

Step 3. Now we can write our own code within the “onHandleIntent“, to handle the background service. Just I added the below statement, to display a log entry; what the code does is, get the message from the intent and display the message in the log entry;

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();
        if ( extras != null ) {
            String msg = extras.getString("Message");
            android.util.Log.d("Greeting! = ", msg);
        }
    }

Step 4. We have the background service in place; we need to add the code to trigger the service. Below is the code it looks like; this is added in MainActivity class, which is visible to the User to enable interaction with the user interface;

    public void doStart(View v) {
        intent = new Intent(MainActivity.this, MyBkgrndService.class);
        intent.putExtra("Message", "Hello, World!");

        startService(intent);
    }

Step 5. Run the application; if everything goes fine, it will display the button, in the center of the window. Click on it, and see the message “Hello, World!” displayed in Logcat window.

That proves, our background service is working fine.

[..] David

Android Programming – Create a background Service

Leave a Reply

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

Scroll to top