Escolar Documentos
Profissional Documentos
Cultura Documentos
Sharif
Chapter Five
INTENTS AND BROADCAST RECEIVERS
Week Eight
Intents & Broadcast receiver
Outline
Introducing Intents
AndroidManifest.xml file
Intent Class and Objects
Intents and App Components
Multiple Activities
Using intents each activity can start other activities
Each time a new activity is started, the previous activity is stopped and the system
The requester sends a description of the operation requested to the Android system, which
locates receiver(s) that are capable of offering the requested service
Allow us to use applications and components that are part of Android System
It also allows other applications to use the components of the applications we create
Through the startActivity(intent), you can define that the Intent should be used to
start an activity. You can also start service via intents, for example
startService(intent)
Start an Activity or Service to perform an action with (or on) a particular piece of
data
Intents
2. Service
Application component that performs long running operations in background with no UI
3. Content Providers
A bridge between applications to share data
10
AndroidManifest.xml file
Every application must have an AndroidManifest.xml file in its root directory
The manifest contains important data about the application that is required by the
Android system before the system will run any of the application's code
11
Purpose of AndroidManifest.xml
There are several purposes of the AndroidManifest.xml:
Contains Java package name of application - unique ID for application
Describes components of application, such as Activities, Services, Broadcast
Receivers, Content Providers and intent messages each component can handle
12
Intent objects carry information, but do not perform any actions themselves
13
Context.startActivity()
Activity.startActivityForResult()
Activity.setResult()
Context.startService()
Context.bindService()
Context.sendBroadcast()
Context.sendOrderedBroadcast()
Context.sendStickyBroadcast()
The Android System finds the right application component to respond to intents,
instantiating them if necessary
UHD-CST Karzan H. Sharif
14
15
16
17
18
19
20
21
22
In fact, the extras can be installed using the putExtras() and getExtras()
methods
23
24
Intent Types
Intent can be divided into two groups:
1. Explicit Intent
specifies the component which should be called by the Android system, usually using
the following code shows how to create an explicit intents and send it to Android
system:
Intent explicitIntent = new Intent(this, ActivityClass.class);
25
specifies the action which should be performed and optionally data that provides data for
the action
26
27
Intent Filter
Applications and components that can receive Implicit Intents advertise what they can
do through Intent Filters
Activities, Services and Broadcast Receivers can have one or more Intent Filters
28
29
30
31
32
33
First Step: the requesting activity creates an Intent object and provides it
with the required information e.g. component name, data, action, .. etc
34
PutExtar & getExtra, For example to get some information like name, surname,
email when user login on Android, and show on second activity these information ,
we can use getExtra and putExtra .
the call
The result will come back through a callback method onActivityResult(int, int,
Intent)
UHD-CST Karzan H. Sharif
35
36
37
38
39
40
41
Broadcast receiver
Broadcast Receivers simply respond to broadcast messages from
other applications or from the system itself.
These messages are sometime called events or intents.
For example, applications can also initiate broadcasts to let other
applications know that some data has been downloaded to the
device and is available for them to use, so this is broadcast
receiver who will intercept this communication and will initiate
appropriate action.
UHD-CST Karzan H. Sharif
42
Broadcast receiver(2)
There are following two important steps to make Broadcast
Receiver works for the system broadcasted intents
Creating the Broadcast Receiver.
Registering Broadcast Receiver
43
44
45
46
47
48
Practice
49
EditText et = (EditText)findViewById(R.id.extraIntent);
// add data to the Intent
intent.putExtra("message", (CharSequence)et.getText().toString());
intent.setAction("com.example.karzan.A_CUSTOM_INTENT");
sendBroadcast(intent);
}
50
51
52
53
54