Android Foreground Service Example

Android Foreground Service

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.:

  1. Music Player
  2. VOIP Calls
  3. File Download
  4. 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:

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:

Next lets define the layout for main activity:

After defining layout it would look something like this:

Android Foreground Service

Let me define the, from where I’ll start the service:

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,

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:

Android Foreground Service - Android Notification with buttons

For a better understanding also have a look at the full source code:

Full Source Code

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.

About Mohit Gupt

An android enthusiast, and an iPhone user with a keen interest in development of innovative applications.

Leave a comment

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

16 thoughts on “Android Foreground Service Example