Reader small image

You're reading from  Android Studio 4.2 Development Essentials - Kotlin Edition

Product typeBook
Published inAug 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781803231549
Edition1st Edition
Languages
Right arrow
Author (1)
Neil Smyth
Neil Smyth
author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor's degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth

Right arrow

66. Implementing an Android Started Service – A Worked Example

The previous chapter covered a considerable amount of information relating to Android services and, at this point, the concept of services may seem somewhat overwhelming. In order to reinforce the information in the previous chapter, this chapter will work through an Android Studio tutorial intended to gradually introduce the concepts of started service implementation.

Within this chapter, a sample application will be created and used as the basis for implementing an Android service. In the first instance, the service will be created using the IntentService class. This example will subsequently be extended to demonstrate the use of the Service class. Finally, the steps involved in performing tasks within a separate thread when using the Service class will be implemented. Having covered started services in this chapter, the next chapter, entitled “Android Local Bound Services – A Worked Example”...

66.1 Creating the Example Project

Select the Create New Project quick start option from the welcome screen and, within the resulting new project dialog, choose the Empty Activity template before clicking on the Next button.

Enter ServiceExample into the Name field and specify com.ebookfrenzy.serviceexample as the package name. Before clicking on the Finish button, change the Minimum API level setting to API 26: Android 8.0 (Oreo) and the Language menu to Kotlin. Modify the project to support view binding as outlined in section 18.8 Migrating a Project to View Binding.

66.2 Designing the User Interface

Locate and load the activity_main.xml file in the Project tool window (app -> res -> layout -> activity_main.xml). Right-click on the “Hello World!” TextView and select the Convert view... option from the resulting menu. In the conversion dialog, select the Button view before clicking on Apply. Select the new button, change the text to read “Start Service” and extract the string to a resource named start_service. With the new Button still selected, locate the onClick property in the Attributes panel and assign to it a method named buttonClick.

66.3 Creating the Service Class

Before writing any code, the first step is to add a new class to the project to contain the service. The first type of service to be demonstrated in this tutorial is to be based on the JobIntentService class. As outlined in the preceding chapter (“An Overview of Android Services”), the purpose of the JobIntentService class is to provide the developer with a convenient mechanism for creating services that perform tasks asynchronously within a separate thread from the calling application.

Add a new class to the project by right-clicking on the com.ebookfrenzy.serviceexample package name located under app -> java in the Project tool window and selecting the New -> Kotlin Class/File menu option. Within the resulting dialog, name the new class MyJobIntentService and select Class from the list. Finally, tap the keyboard Return key to create the new class.

Review the new MyJobIntentService.kt file in the Android Studio editor where...

66.4 Adding the Service to the Manifest File

Before a service can be invoked, it must first be added to the manifest file of the application to which it belongs. At a minimum, this involves adding a <service> element together with the class name of the service and the BIND_JOB_SERVICE permission request.

Double-click on the AndroidManifest.xml file (app -> manifests) for the current project to load it into the editor and modify the XML to add the service element as shown in the following listing:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.serviceexample" >

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

   &...

66.5 Starting the Service

Now that the service has been implemented and declared in the manifest file, the next step is to add code to start the service when the button is clicked. Locate and load the MainActivity.kt file into the editor and implement the buttonClick() method to add the code to start the service:

package com.ebookfrenzy.serviceexample

.

.

import android.view.View

import androidx.core.app.JobIntentService.enqueueWork

 

class MainActivity : AppCompatActivity() {

 

    val SERVICE_ID = 1001

.

.

    fun buttonClick(view: View)

    {

        enqueueWork(this, MyJobIntentService::class.java, SERVICE_ID, intent)

    }

}

The code declares a service id (this can be any integer value) which is used to identify the service. Next, the buttonClick() method is implemented to call enqueueWork() method...

66.6 Testing the IntentService Example

The example IntentService based service is now complete and ready to be tested. Since the messages displayed by the service will appear in the Logcat panel, it is important that this is configured in the Android Studio environment.

Begin by displaying the Logcat tool window before clicking on the menu in the upper right-hand corner of the panel (which will probably currently read Show only selected application). From this menu, select the Edit Filter Configuration menu option.

In the Create New Logcat Filter dialog name the filter ServiceExample and, in the Log Tag field, enter the TAG value declared in MyIntentService.kt (in the above code example this was ServiceExample).

When the changes are complete, click on the OK button to create the filter and dismiss the dialog. The newly created filter should now be selected in the Android tool window.

With the filter configured, run the application on a physical device or AVD emulator session...

66.7 Using the Service Class

While the JobIntentService class allows a service to be implemented with minimal coding, there are situations where the flexibility and synchronous nature of the Service class will be required. As will become evident in this chapter, this involves some additional programming work to implement.

In order to avoid introducing too many concepts at once, and as a demonstration of the risks inherent in performing time-consuming service tasks in the same thread as the calling application, the example service created here will not run the service task within a new thread, instead relying on the main thread of the application. Creation and management of a new thread within a service will be covered in the next phase of the tutorial.

66.8 Creating the New Service

For the purposes of this example, a new class will be added to the project that will subclass from the Service class. Right-click, therefore, on the package name listed under app -> java in the Project tool window and select the New -> Service -> Service menu option. Create a new class named MyService with both the Exported and Enabled options selected.

The minimal requirement in order to create an operational service is to implement the onStartCommand() callback method which will be called when the service is starting up. In addition, the onBind() method must return a null value to indicate to the Android system that this is not a bound service. For the purposes of this example, the onStartCommand() method will loop 3 times sleeping for 10 seconds on each loop iteration. For the sake of completeness, stub versions of the onCreate() and onDestroy() methods will also be implemented in the new MyService.kt file as follows:

package com.ebookfrenzy...

66.9 Launching the Service

Edit the MainActivity.kt file and modify the buttonClick() method to launch the MyService service:

package com.ebookfrenzy.serviceexample

.

.

import android.content.Intent

 

class MainActivity : AppCompatActivity() {

.

.

    fun buttonClick(view: View)

    {

        intent = Intent(this, MyService::class.java)

        startService(intent)

    }

}

All that the buttonClick() method does is create an intent object for the new service and then start it running.

66.10 Running the Application

Run the application and, once loaded, touch the Start Service button. Within the Logcat tool window (using the ServiceExample filter created previously) the log messages will appear indicating that the buttonClick() method was called and that the loop in the onStartCommand() method is executing.

Before the final loop message appears, attempt to touch the Start Service button a second time. Note that the button is unresponsive. After approximately 20 seconds, the system may display a warning dialog containing the message “ServiceExample isn’t responding”. The reason for this is that the main thread of the application is currently being held up by the service while it performs the looping task. Not only does this prevent the application from responding to the user, but also to the system, which eventually assumes that the application has locked up in some way.

Clearly, the code for the service needs to be modified to perform tasks...

66.11 Using a Coroutine for the Service Task

As outlined in “A Basic Overview of Threads and AsyncTasks”, when an Android application is first started, the runtime system creates a single thread in which all application components will run by default. This thread is generally referred to as the main thread. The primary role of the main thread is to handle the user interface in terms of event handling and interaction with views in the user interface. Any additional components that are started within the application will, by default, also run on the main thread.

A very simple solution to this problem involves performing the service task within a Kotlin Coroutine. Start by editing the Gradle Scripts -> build.gradle (Module: ServiceExample.app) file to add the following lines to the dependencies section):

dependencies {

.

.

    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.1'

    implementation...

66.12 Summary

This chapter has worked through an example implementation of an Android started service using the IntentService and Service classes. The example also demonstrated the use of asynchronous tasks within a service to avoid making the main thread of the application unresponsive.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android Studio 4.2 Development Essentials - Kotlin Edition
Published in: Aug 2021Publisher: PacktISBN-13: 9781803231549
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime

Author (1)

author image
Neil Smyth

Neil Smyth has over 25 years of experience in the IT industry, including roles in software development and enterprise-level UNIX and Linux system administration. In addition to a bachelor's degree in information technology, he also holds A+, Security+, Network+, Project+, and Microsoft Certified Professional certifications and is a CIW Database Design Specialist. Neil is the co-founder and CEO of Payload Media, Inc. (a technical content publishing company), and the author of the Essentials range of programming and system administration books.
Read more about Neil Smyth