Android Service

Posted By on March 23, 2016


Download PDF
Open Web Browser in Android with Specific URL
Communication Between Service and Activity in Android

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 /srcfolder or just copy and paste the content.

Here We have created a new class called MySercive which is extended from Android inbuiltService 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.

Open Web Browser in Android with Specific URL
Communication Between Service and Activity in Android

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com