Broadcast Receiver and Service
Broadcast Receiver and Service
4W1H
Receiver
Sending
Completely asynchronous Delivered to one receiver at a time
Broadcast Receiver
Broadcast Receiver Lifecycle
A Broadcast Receiver is an application class that listens for Intents that are
broadcast, rather than being sent to a single target application/activity.
The system delivers a broadcast Intent to all interested broadcast
receivers, which handle the Intent sequentially.
9
Broadcast Receiver
Broadcast Receiver Lifecycle
A broadcast receiver has a single callback method:
1.When a broadcast message arrives for the receiver, Android calls its
onReceive()method and passes it the Intent object containing the message.
10
Broadcast Receiver
Registering a Broadcast Receiver
1. You can either dynamically register an instance of this class with
Context.registerReceiver()
2. or statically publish an implementation through the <receiver> tag in your
AndroidManifest.xml.
Manifest
the application defines a BroadcastReceiver as an independent class, it
must include a <receiver> clause identifying the component. In addition an
<intent-filter> entry is needed to declare the actual filter the service and
the receiver use.
11
Broadcast Receiver
Manifest
12
Broadcast Receiver
Types of Broadcasts
abortBroadcast() 13
Broadcast Receiver
Standard Broadcast given by the platform
AIRPLANE_MODE
ACTION_TIME_TICK
ACTION_TIME_CHANGED
ACTION_TIMEZONE_CHANGED
ACTION_BOOT_COMPLETED
ACTION_PACKAGE_ADDED
ACTION_PACKAGE_CHANGED
ACTION_PACKAGE_REMOVED
ACTION_PACKAGE_RESTARTED
ACTION_PACKAGE_DATA_CLEARED
ACTION_UID_REMOVED
ACTION_BATTERY_CHANGED
ACTION_POWER_CONNECTED
ACTION_POWER_DISCONNECTED
ACTION_SHUTDOWN
14
Broadcast Receiver
Sender
Send broadcast
Action
Receiver
Any activity that intends to respond to broadcasts has to have class extend
the android.content.BroadcastReceiver and implement the single method
onReceive()
Manifest file
Action
15
Broadcast Receiver
Register BroadCast Receiver by coding
16
Broadcast Receiver
Register BroadCast Receiver by Manifest XML
17
Broadcast Receiver
Layout XML
18
Broadcast Receiver MySmsReceiver class
19
Broadcast Receiver MainActivity class
20
Broadcast Receiver MainActivity class
21
Broadcast Receiver Manifest XML
Automatic go to onReceive
SMS come in
22
Services
Run in the main thread of their hosting process. This means that
31
Services
singleton
Only one stopService( )call is needed to stop the service, no matter how
many times startService() was called.
A service can be started and allowed to run until someone stops it or it stops
itself.
32
Services
Service Life Cycle
Like activity
has lifecycle methods to monitor changes in its state.
1.void onCreate()
2.void onStart(Intentintent)
3.void onDestroy()
33
Services
Service Life Cycle
Like an activity, a service does its initial setup in onCreate(), and releases
all remaining resources in onDestroy().
For example, a music playback service could create the thread where the music
will be played in onCreate(), and then stop the thread in onDestroy().
34
Services
35
Services
36
Services Example 1:
37
Services Example 1:
38
Services
Broadcast
Binder
Messenger
39
Services Using Broadcast
Assume main activity MyService3Driver wants to interact with a service
called MyService3. The main activity is responsible for the following tasks:
40
Services Using Broadcast
41
Services Using Broadcast
42
Services Example 2 - Broadcast
2.The service is slow, therefore it runs in a parallel thread its time consuming task.
3.When done with a computing cycle, the service adds a message to an intent.
5.A BroadcastReceiver(defined inside the main Activity) uses the previous filter
and catches the message (displays the contents on the main UI ).
6.At some point the main activity stops the service and finishes executing
43
Services Example 2 - Broadcast
XML Layout
44
Services Example 2 - Broadcast
45
Services Example 2 - Broadcast
46
Services Example 2 - Broadcast
47
Services Binding service
local binding
48
Services Using Messager
49
https://developer.android.com/
50