Simple Android Service Example Code Description(Start/Stop Service)

Simple Android Service Example with Code Description

Here we will learn about Android Service Example with detailed code description. Also you can download Android service example source code from here. Lets first discuss what exactly Android service means? Then will discuss different types of Android services, Then we will go for complete Android Service Example and code description.

 What is Android Service ?

Service is another component of an Android application, which does not provide any type of GUI. It is a program that can run in the background for an indefinite period. That means if we want to do a long operation (example: download data from internet), then we need to create an Android service for this purpose. Please keep in your mind that Android Service is neither a Thread or nor a Process. I have written a very good article on basic Android fundamental/components and their lifecycle  (Activity, Service, Broadcast Receiver, Content Provider etc). You might like that article. Please click here to read.

Generally There are two types of Android Services.

  • Started Service (Unbounded)

This type of service is created and called by Android Activities. There is no 2 way communication between Android Activity and Service. The Activity just started the service and does not care about the status of the service.

  • Bound Service (Bounded)

This type of Android Service is for two-way communication. Suppose an Android Activity has started a bound service, then Activity can be notified the status at any given time from the service. A bound service is a client-server interface that allows  two-way communication, send requests, get results, and even work for across processes using interprocess communication (IPC)

Here We will discuss about a very simple Android started service (unbounded) with a very simple example, then we will discuss about Android Bounded Service with example in a separate example. So this Android service example is just the demonstrate for how to start and stop a android service from an activity.

android service exampleandroid service example

 Create/Start /Stop an Android Service from an Activity.

Step 1: Create an Android Service

Create an Android service class (Example: MyService) by extending the Android inbuilt class Service like below.

 Step 2: Override Android Service Functions

@Override/implement below virtual functions in your class like below.

 Step 3: Register Android Service class in the Manifest.xml file

Step 4: Start Android Service using startService() function

Start the service from an Activity like below.

 Step 5: Stop Android Service using stopService() function

Stop the service from an Activity like below

Project Creation:  Android Service Example (Complete Code)

To make this example very simple, we just add 2 buttons (‘Start Service’ and ‘StopService’) on the main Activity GUI, We will start a new service when click on the button Start Service and stop the running service when click on the button Stop Service and show the service status using an Android toast message.

1. Create a new project with project name: ServiceExample

2. Fill Application name: ServiceExample

3. Fill Package Name as: com.techblogon.serviceexample

4. I have used SDK version Android 4.0.3 and Eclipse Version Indigo. But you can use any version.

5. Add below xml file (activity_main.xml) into your project’s res/layout folder. or you can copy the xml file contents.

 6. Now add a new java file for your  Service Class code (MyService.Java) to your projects /src folder or just copy and paste the content.

Here We have created a new class called MySercive which is extended from Android inbuilt Service class. Then in that class we have @ Override some required virtual functions like ( onBind(), onCreate(), onStart(), onDestroy() etc).

 7. Register your service (MyService class) in the Manifest file. Add the below Manifest file in the root folder of your project or just copy and paste the content.

 8. Call Start Service and Stop Service from the main activity class.

Notes:

  1. When we start an Android Service by calling the function startService(), then the execution flow in the Service class will be from onCreate() to onStart() function. Similarly when we call stopService() function, the onDestroy() function will be call from the service class.
  2. stopService() will stop the service no matter how mnay times you called startService(). 
  3. If the service is already running and you started the service again, then its onStartCommand() is also called. So your service class must be prepared that onStartCommand() can be called several times. 

9. Build and Run the code on Emulator/Device. Then click Start Service and Stop Service buttons to test your Android Service Example.

download

Download Complete Android Service Example Source Code from Here.

For complete usage of Android service example and code description (activity-service communication), I have already written a very nice article. For Android Service Example Code Description (Complete Tutorial) click here.

I hope this small simple tutorial will give you some idea about Android Services. I will come up with an article very soon on Android Services Example with a real scenario.

 

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect

11 Responses to Simple Android Service Example Code Description(Start/Stop Service)

  1. Carlos says:

    excelent blog !!!

  2. ok blog says:

    ok blog…

    Have a fantastic day!I’m very pleased when see your post.I quite endorse your standpoint.I will continue to following on your blog.I confirm that the future I will see more about your good views….

  3. Kunal Shah says:

    Excellent blog ! Too good for beginners..

  4. bhupalreddy says:

    its very nice example for begginers…i impressed a lot about this simple example

  5. sustecha says:

    hello ms.SmrutiRanjan it’s really rich example after downloding your code ,
    i have error on running it couldnot find mainActivity.apk ??
    hope to run it as soon as possible ;)

  6. ammulya says:

    excellent job ,very easy 2 understand.can u give me any real time example on service with explanation?

  7. mark says:

    Hello SmrutiRanjan, I like your blog entries.
    I have some questions about device drivers and android which I hope you can help me with. When Qualcom updates the Adreno 2xx drivers for the Galaxy SII, they indicate that they are user-mode drivers. Do you know why there aren’t usually any updates to the kernel mode driver? and how do can I install these drivers? Maybe you can point me some links showing how the user + kernel mode drivers differ (and what parts are open source)

    Thanks

  8. karim says:

    nice, clear and short.
    THANK YOU.

  9. Mitul Gedeeya says:

    Hello
    Really it;s very very Awesome Tutorial for beginner
    and of Course i m beginner so i can say this is very easy and most useful tutorial for persons who wants to begin Services in Android…

  10. Jona says:

    Thanks for the nice post you made.

    My request one thing.
    Please do differentiate code between “Started Service” and “Bound Service”

Leave a Reply

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


9 − = eight

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">