Android Foreground Service Example

Android guidelines suggest that a long running operation should be performed in a service. As that long running operation may not require a UI and Android Services do not provide a UI. But what if that long running operation requires a UI or maybe just two or three pre-defined inputs. The first solution that comes in my mind is Android Foreground Service. This type of service falls under the category of started service, as its not bound to any component and neither it returns any result back to the caller.

Introduction to Android Foreground Service

Lets start with a basic know hows; As you may know that services are used for time consuming operations which do not require a user interface. But there are situations when long running operations are in progress and you would want the user to know progress of that operation. E.g.:

Music Player

VOIP Calls

File Download

and many more

Services have a unique property; once ‘started’ they can run even if you put the app in background. All the above listed operations are similar sort of operations, once started they don’t need a UI to complete. But for good user experience you would want the user to know that, this sort of operation is going on. Also now notifications in android allow for three action buttons, through which the user can interact with the ongoing operation if they want. Therefore the best suitable approach for these sort of operations is an Android Foreground Service.

In this tutorial for Android foreground service, I would make a music player stub (not an actual player). Here the user would be able to start a foreground service from an activity by calling startService() method. After this when onStartCommand() method is invoked in service class, I would call the actual startForeground() method. By doing this Android would fire a notification, and from now on this service would be called as an Android Foreground Service. The interesting part here would be that after starting the service we can actually close the activity and interact with service through the notification buttons, or notification actions. To start off lets have a look at the App Manifest:

AndroidManifest.xml

XHTML

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

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

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

package="com.truiton.foregroundservice"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="11"

android:targetSdkVersion="19" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name=".MainActivity"

android:label="@string/app_name"

android:launchMode="singleTask" >

<intent-filter>

<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

<service android:name=".ForegroundService" >

</service>

</application>

</manifest>

Point to be noted here is that the main activity is launched in android:launchMode="singleTask", as whenever we launch this activity we want only once instance of it at task root level. In this Android Foreground Service Example I have used a constants file to keep the code clean. In any project, as a good coding practice it is advised that all constants are kept in a single class, as its a little easier to access. Have a look at the constants file for this example:

As you can see in the above piece of code, I have defined the objects for two buttons and on click of first button I am starting the service in foreground mode, and on click of second button I have written the code to stop the service. Now lets define the main class of this Android Foreground Service Example, ForegroundService.java:

Please Note:R.drawable.truiton_short is just an image I used for displaying the notification large icon in setLargeIcon method, you can use any other drawable or bitmap like R.drawable.ic_launcher.

In the above class, as you can see I am starting the foreground service by calling startForeground() method. This is the point when a normal service transforms into a foreground service. Also please keep in mind that when a foreground service is stopped by calling the stopForeground() method it does not stop the service, it just removes the service from foreground mode. To stop the service you may have to call the stopSelf() method. To start a service in foreground mode, you need to create an Android notification with notification id. So lets have a closer look at the notifications:

Android Notification with Button

In this Android Foreground Service Example a special type of notification is used. This type of notification can perform actions, and they are called Android notification actions. Please have a look at the screen shot below:

You may observe that, above notification has three buttons/actions attached to it. To do this, one has to use the addAction method, with appropriate pending intent. If you closely observe the above class, getService method is used for pendingIntents of notification actions. By using this method my intent would go directly to the onStartCommand() method of my ForegroundService. This would deliver my intent directly to the service instead of going through an activity. The advantage of this approach is that, I don’t need to be dependent on my activity. This truly makes this service a foreground service. Hope this Android Foreground Service Example helped.

Thank you for your Tutorial. Theres one thing i want to point out, it’s not recommended to call .equals() on intent.getAction() because it might be null, instead calling it on the constant is a better way to do this.