0% found this document useful (0 votes)
68 views4 pages

Android's Bound Services - What You Should Know

This document provides an overview of bound services in Android. It explains that bound services allow clients like activities to interact with a service. Clients bind to the service using bindService() and receive an interface to communicate. They can unbind by calling unbindService(). The service will run until all clients unbind and may be stopped or kept running by the system depending on how it was started.

Uploaded by

noidsonly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views4 pages

Android's Bound Services - What You Should Know

This document provides an overview of bound services in Android. It explains that bound services allow clients like activities to interact with a service. Clients bind to the service using bindService() and receive an interface to communicate. They can unbind by calling unbindService(). The service will run until all clients unbind and may be stopped or kept running by the system depending on how it was started.

Uploaded by

noidsonly
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

10/21/2016

Android'sboundServices:Whatyoushouldknow

(https://play.google.com/store/apps/details?id=idig.za.net.addiction)

Free Android app covering all aspects of addiction, including prevention and treatment
You are here: Home (/) / Tutorials (/index.php/latest.html) / Articles (/index.php/itemlist/category/8-articles.html) / Bound Services: What you should know!

Tweet

Checkoutourgoogle+page

Like

Send

Share

(https://plus.google.com/+101appsCoZa/?prsrc=3)

Share

Bound Services: What you should know!

Written by Clive

Build the Perfect API


Learn modern design techniques for building the perfect API. Go to mulesoft.com/Build_Perfect_API

Bound Services: a primer

Quick recap on Services


Services are app components that you can use to do work in the background.
Services run in your apps process in the main thread. Dont block the main thread. Use a separate thread to do any heavy work in your
Service.
You can also use Services running in another apps process.
Services continue to run until you stop them. The system can also kill them at any time but you can set their priority so that they are unlikely
to be killed.
So what happens when we bind to a Service?

http://www.101apps.co.za/articles/boundserviceswhatyoushouldknow.html

1/4

10/21/2016

Android'sboundServices:Whatyoushouldknow

A bound Service enables the client to interact with the Service


If you have not already done so, you may want to look at the following articles and tutorials:
All about Services (/articles/all-about-services.html)
Services Tutorials: Part 1. A simple Service (/articles/services-tutorials-part-1-a-simple-service.html)
Services Tutorials: Part 2. A foreground Service (/articles/services-tutorials-part-2-a-foreground-service.html)
This article covers the basics of bound Services. Our tutorial, which follows, puts everything together. You'll see how easy it is to use a bound Service in your apps!

Taking control: a bound Service


There are two types of Services:
A started Service (/articles/services-tutorials-part-1-a-simple-service.html) one of your app components starts the Service
A Bound Service the Service acts as a server. Your apps component (the client), logs in (binds) to the server, uses the server to do some work and then logs
out (unbinds)
You can use either or both types at the same time.
The Service could already be running when you bind to it and it may continue to run after you unbind from it.
If the Service is not running, then it will start when you bind to it and stop when you unbind. As more than one client can bind to a Service at the same time, the
Service will remain running until the last client has unbound from it.
Clients from other apps can also bind to your Service if you allow them to. You can also bind to other apps Services if they allow you to.

Why use a bound Service?


You would use a started Service to do work for you if you dont have to interact with the Service again.
You would use a bound Service if you need to interact with the service at some stage. You could also use a bound Service if you want other apps to be able to
interact with parts of your app.
Youll need an interface to interact with the Service.

The three types of Service Interface


Client and Service in same process
The Service and client are in the same process. Get the interface by extending the Binder class. Its returned by onBind(). The client then uses this
interface to access the public methods in the Service. This is the most common type. Well show how its done in our tutorial
Client and Service in dierent processes
Use the following types of interface if your Service is used by other applications or across separate processes.
use a messenger to interact with a Service across processes. The messenger creates a queue in a single thread for all the clients requests. The
requests are then dealt with one at a time
use AIDL if you want your Service to handle multiple requests at the same time

http://www.101apps.co.za/articles/boundserviceswhatyoushouldknow.html

2/4

10/21/2016

Android'sboundServices:Whatyoushouldknow

A bound Service is commonly used to interact with a Service when both client and Service are in the same process

The Clients
Clients are app components that bind to the Service.
These are the clients that can bind to a Service:

Activities
Services
Content Providers
You cant bind to a Service from:
Broadcast Receivers
The Service automatically disconnects, or unbinds from the Service when the client is destroyed (unless the Service was started with startService(), in which case it
will continue running until called to stop). Bear in mind that an activity is destroyed when the device changes orientation.
You should manually unbind from the Service when you dont need it. For example, unbind from the Service when your activity pauses so that the Service can shut
down and release its resources.

The ties that bind: Binding to the Service


Use bindService() to bind to a Service. This will connect to a running service. It creates the Service if it is not running.
bindService() receives three parameters:
intent identies the Service to connect to
the Service connection object
ags these specify the options for binding:
BIND_AUTO_CREATE binds to an existing Service, creating the Service if it does not exist. onStartCommand() is not called
BIND_DEBUG_UNBIND should only be used for debugging
BIND_NOT_FOREGROUND wont allow the Services priority to be raised to that of foreground priority (/articles/services-tutorials-part-2-a-foregroundservice.html). Its priority will only be raised to that of the clients priority. This is only important if the clients process is in the foreground and the
Services process is in the background
Can be 0
After the client calls bindService(), it receives an IBinder object which it uses to interact with the Service.
You can bind to an existing Service. This Service will run until it is called to stop, even if all the clients have unbound.
If it is a Service started with a call to bindService() then it will stop once all the clients have unbound.

Cut all ties: Unbinding from a Service


Multiple clients can bind to a service at once.
Call unbindService() to unbind. Once all the clients have unbound, the Service is destroyed (if the Service was started by a call to startService, then it must be
stopped by a call to stopSelf() or stopService()).

http://www.101apps.co.za/articles/boundserviceswhatyoushouldknow.html

3/4

10/21/2016

Android'sboundServices:Whatyoushouldknow

If you connected to a running Service using the BIND_AUTO_CREATE , and you call stopService, the Service will not stop until all these clients have unbound.
onDestroy() is called when the Service stops. This is where you can release any resources by stopping threads and unregistering receivers for example.

Time is money: keep the Service running


Also, when using the ag, BIND_AUTO_CREATE to start a Service to bind to, the Service will not necessarily stop once all the clients have unbound. The system may
allow the Service to continue running until its resources are needed elsewhere. Only then will it will be killed. Its cheaper to keep the Service running in case its
needed again than to kill it and then restart it.
If you start a thread in your Service and unbind from that Service, the process in the thread will continue to run. You need to stop it to conserve resources.

The right time: When to bind and when to unbind


You should match your binding and unbinding to a Service with the clients appropriate lifecycle methods:
If you need to interact with the Service while your activity is visible, then bind in onStart() and unbind in onStop()
If you need to interact with the Service even when its stopped, then bind in onCreate() and unbind in onDestroy()
You should usually not bind in onResume() and unbind in onPause()

The Bound Services Lifecycle


You dont have to manage the lifecycle of the Service if its purely a bound Service. The system destroys it once all clients have unbound.
If you started a Service then you must stop it, regardless of whether any clients are bound to it.
Have a look at Binding to a Service: A Tutorial (/articles/binding-to-a-service-a-tutorial.html), where we show you how to bind to a Service to control a media player.
I hope that you have found this tutorial helpful.

Related items
Realm databases for Android beginners (/index.php/item/186-realmdatabases-for-android-beginners.html)
Addiction 101:Help for addicts (/index.php/item/185-addiction-101-help-foraddicts.html)
Open Images Gold: Version 6 help (/index.php/item/184-open-images-goldversion-6-help.html)
Free Clip Art Gold, version 2.1 upgrade (/index.php/item/183-free-clip-artgold-version-2-1-upgrade.html)
Android Image editing app (/index.php/applications/android-image-editingapp.html)

http://www.101apps.co.za/articles/boundserviceswhatyoushouldknow.html

4/4

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy