CH 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Jimma University

JIT Mobile Application


Faculty of computing development
&Informatics

Chapter Three
Intents and Services

1
Chapter Objectives
At the end the chapter students should able to know:

 • Describe Intents and services

 • Understand when and how to use Intents and services

2
Android Intent
 Android Intent is an object carrying an intent .

 Intent is the message that is passed between components


, such as activities, content providers, broadcast receivers,
services etc, with-in the application or outside the
application.

 The intent itself, an Intent object, is a passive data


structure holding an abstract description of an operation to be
performed.

3
Android Intent

Android intents are mainly used to:

• Start the service


• Launch an activity
• Display a web page
• Display a list of contacts
• Broadcast a message
• Dial a phone call etc.
4
Android Intent
Component Description

Starting an activity By Sending Intent object to


startActivity() method we can start
new activity

Starting a service By Sending Intent object to


startService() method we can start
new service or send required instruction
to an existing service

Delivering broadcast By Sending Intent object to


startBroadcast() method we can
deliver our message to other app
broadcast receiver

5
Intent

• Component: name of application


component
• Action : general task that need to be
performed by the component
• Category: it's a string containing
additional information about the kind of
component that should handle the intent.
• Data: URI
Android intent my • Extras: additional information that
take the following should be delivered to the component(
parameters key value pairs)
• Flags: instruct the Android system how
to launch an activity, and how to treat it
after it's launched etc

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:

 This is useful when your applications is not capable of doing it but it


is important action

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

Foreground Background Bound

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

 Example : Syncing and storing data

 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

You might also like