CH 3
CH 3
CH 3
Chapter Three
Intents and Services
1
Chapter Objectives
At the end the chapter students should able to know:
2
Android Intent
Android Intent is an object carrying an intent .
3
Android Intent
5
Intent
6
Types of Intent
Intent
Explicit Implicit
7
EXPLICIT INTENTS
These intents designate the target component by its
name and they are typically used for application-internal
messages
such as an activity starting a subordinate service or
launching a sister activity(application component)
8
EXPLICIT INTENTS
The target component which receives the intent can use
the getExtras() method to get the extra data sent by
the source component. For example:
9
IMPLICIT INTENTS
These intents do not name a target and the field for the component
name is left blank.
Implicit intents are often used to activate components in other
applications. For example:
10
Services
A service is a
For example, a
component that runs or it might fetch data don’t need to
service might play
in the background to over the network provide some UI for
music in the
perform long-running without blocking the operations to be
background while the
operations without user interaction with performed in the
user is in a different
needing to interact an activity. background.
application,
with the user.
11
Service Types
Service
Types
13
Foreground Service
Foreground Service are services that visible to users
It is used for operation that is noticeable (status bar
notification) to the user.
Foreground services continue running even when the
user isn't interacting with the app.
A perfect example is Music player and downloading
This notification cannot be dismissed unless the service is
either stopped or removed from the foreground.
14
Background Service
A background service performs an operation that
isn't directly noticed by the user.
Service that run background, such that the user cant see or access
them
These are the tasks that don’t need the user to know
But for the API level 21 or higher, the Android System imposes
some restrictions while using the Background Service.(Be
aware before using it)
15
Bound Service
Bound Services run as long as some other application
component bind to it.
A service is bound when an application component binds to it
by calling bindService() method.
This type of service runs until other application components
are attached, or bound, to them.
Multiple components can be bound to a service and in this
case, the service only dies when all of these multiple
components unbind from the service.
16
Life cycle of Android Services
No. State & Description
1 Started
A service is started when an application component, such as an
activity, starts it by calling startService(). Once started, a service can
run in the background indefinitely, even if the component that
started it is destroyed.
2 Bound
A service is bound when an application component binds to it by
calling bindService(). A bound service offers a client-server interface
that allows components to interact with the service, send requests,
get results, and even do so across processes with interprocess
communication (IPC)
17
Life cycle of Android Services
onRebind()
18
Methods of Android Services
Callback Description
onStartCommand() The system calls this method when another component, such as an
activity, requests that the service be started, by calling
startService(). If you implement this method, it is your
responsibility to stop the service when its work is done, by calling
stopSelf() or stopService() methods.
onBind() The system calls this method when another component wants to
bind with the service by calling bindService(). If you implement this
method, you must provide an interface that clients use to
communicate with the service, by returning an IBinder object. You
must always implement this method, but if you don't want to allow
binding, then you should return null.
onUnbind() The system calls this method when all clients have disconnected
from a particular interface published by the service.
19
Methods of Android Services
Callback
Callback Description
onRebind() The system calls this method when new clients have connected to
the service, after it had previously been notified that all had
disconnected in its onUnbind(Intent)
onCreate() The system calls this method when the service is first created
usingonStartCommand() or onBind(). This call is required to
perform one-time setup.
onDestroy() The system calls this method when the service is no longer used
and is being destroyed.Your service should implement this to clean
up any resources such as threads, registered listeners, receivers,
etc.
20
21
Reading assignment
22