Android Service Tutorial

Android Service is a component that running in the background without a user interface. It is a long running operation and does not need to interact with the user. Besides, android service is mostly use for specific situation such as download files in the internet and listening music. There have 2 type of services which are started and bound. Android Start Service is started when calling startservice() method by an application component (eg. Activity). Once the service started, it will run in the background forever and cannot be destroy without calling stopservice method. Android Bind Service is bound when an application component (eg activity) binds to it by calling bindService() method. It offers a client-server interface and allows components to interact with the service. For example, service process calculation and updating the activity ui. The bound service able to return a value or result to the user which started service cannot perform.

Both have different behaviour and life cycle. You can use either startService() or bindService() to start the android service. If you create a service by binding to it, it will die when you unbind the service. To keep the service working around without component activities binding to it is to start it with startService(). The started service is no conflict to the application lifecycle because it does not bind to any activity. Therefore, you are able to bind and unbind to it as much as you wish and it will only die when you call stopService(). In this tutorial, I will show you how the activity interacts with the android service and transmit data in android service.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “ServiceExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Create a new Service

Add the service class in your package. Right click your package name click New > Service > Service. It will pop up a windows and click finish button. (Optional: you can change service class name).

Android Service Picture 1

Once service class created, you will see <service> is include in your AndroidManist.xml.

Edit MyService.java class

Go to your MyService.java add the following code. I add the callback interface to display the internal message to the activity. In the thread need to use handler to update UI in the activity.

IBinder onBind(Intent intent)

– It will called when the activity bind to the service and return the communication channel to the service.

For below example I return a new Binder for this method. I overwrite new Binder which using the class Binder that extends android.os.Binder.

int onStartCommand(Intent intent, int flags, int startId)

– It will called by everytime explicitly start the service by calling startService().

Edit activity_main.xml layout

Add the textView, editText and 5 buttons in your activity_main.xml. The 5 button perform the feature such as start,stop,bind,unbind and synchronise service.

Edit MainActivity.java class

In your MainActivity.java insert the following code. You should implements View.OnClickListener and ServiceConnection in your activity. If you want to use the bind service you need to implement ServiceConnection interface and override two method.

void onServiceConnected(ComponentName name, IBinder service)
– This method will called when the bind service is connected.
void onServiceDisconnected(ComponentName name)
– This method will called when the connection of bind service is lost, killed or crashed.

Run your project

Now, you can start your project and try the android service now.

(Android Service)

Source Code

(Visited 574 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

Leave a Reply

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