Android –
BroadcastReceiver
Yong Heui Cho @ Mokwon University
Some of slides are referred to:
[1] Everything of Android, slideshare.
2
Broadcast Receiver
• An Intent-based publish-
subscribe mechanism.
• Great for listening system
events such as SMS messages.
□ Courtesy to Everything of Android, slideshare.
3
Uses of BR
• A broadcast receiver is a component that
does nothing but receive and react to
broadcast announcements
• Your app can
– Receive and react to system services (example:
battery low)
– Receive and react to other apps broadcast
announcements
– Initiate broadcasts to other apps
□ Courtesy to Everything of Android, slideshare.
4
Register of BR
• To register Broadcast Receiver, you can
– Dynamically register with registerReceiver (in
code)
– Using <register> tag in AndroidManifest.xml
□ Courtesy to Everything of Android, slideshare.
5
Send of Broadcast
• Broadcast is sent using Intent and
sendBroadcast or sendOrderedBroadcast
methods
• Example:
– Intent intent = new
Intent(“com.enlume.MSG_PRO");
sendBroadcast(intent);
□ Courtesy to Everything of Android, slideshare.
6
Normal vs. Ordered
• Normal Broadcasts
– Sent with sendBroadcast. All broadcasts are
run in undefined order, often at the same
time.
• Ordered Broadcasts
– Sent with sendOrderedBroadcast. Each
receiver executes in turn. Possible to
propagate a result to next receiver. Order can
be controlled using android:priority tag.
□ Courtesy to Everything of Android, slideshare.
7
BR Lifecycle
• Broadcast Receiver object is valid only for
the duration of the onReceive(Context,
Intent) method.
– You cannot do anything asynchronous in
here!
– Except you can start a service, which can start
a thread.
□ Courtesy to Everything of Android, slideshare.
8
Event Listener
• Interface to process specific events
9
BR vs. Listener
type BroadcastReceiver Listener
goal class for Intents
class or interface
for Intents &
others
message
mostly global
messages
mostly specific
messages
period
discontinuous
reception
frequent (or
continuous) reception
construction
static (manifest) or
dynamic
dynamic only

Android - Broadcast Receiver

  • 1.
    Android – BroadcastReceiver Yong HeuiCho @ Mokwon University Some of slides are referred to: [1] Everything of Android, slideshare.
  • 2.
    2 Broadcast Receiver • AnIntent-based publish- subscribe mechanism. • Great for listening system events such as SMS messages. □ Courtesy to Everything of Android, slideshare.
  • 3.
    3 Uses of BR •A broadcast receiver is a component that does nothing but receive and react to broadcast announcements • Your app can – Receive and react to system services (example: battery low) – Receive and react to other apps broadcast announcements – Initiate broadcasts to other apps □ Courtesy to Everything of Android, slideshare.
  • 4.
    4 Register of BR •To register Broadcast Receiver, you can – Dynamically register with registerReceiver (in code) – Using <register> tag in AndroidManifest.xml □ Courtesy to Everything of Android, slideshare.
  • 5.
    5 Send of Broadcast •Broadcast is sent using Intent and sendBroadcast or sendOrderedBroadcast methods • Example: – Intent intent = new Intent(“com.enlume.MSG_PRO"); sendBroadcast(intent); □ Courtesy to Everything of Android, slideshare.
  • 6.
    6 Normal vs. Ordered •Normal Broadcasts – Sent with sendBroadcast. All broadcasts are run in undefined order, often at the same time. • Ordered Broadcasts – Sent with sendOrderedBroadcast. Each receiver executes in turn. Possible to propagate a result to next receiver. Order can be controlled using android:priority tag. □ Courtesy to Everything of Android, slideshare.
  • 7.
    7 BR Lifecycle • BroadcastReceiver object is valid only for the duration of the onReceive(Context, Intent) method. – You cannot do anything asynchronous in here! – Except you can start a service, which can start a thread. □ Courtesy to Everything of Android, slideshare.
  • 8.
    8 Event Listener • Interfaceto process specific events
  • 9.
    9 BR vs. Listener typeBroadcastReceiver Listener goal class for Intents class or interface for Intents & others message mostly global messages mostly specific messages period discontinuous reception frequent (or continuous) reception construction static (manifest) or dynamic dynamic only