0% found this document useful (0 votes)
36 views42 pages

Broadcast Receiver and Service

6. Broadcast receiver and Service

Uploaded by

money120994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views42 pages

Broadcast Receiver and Service

6. Broadcast receiver and Service

Uploaded by

money120994
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Broadcast receiver

4W1H

• What is Broadcast Receiver?


o 1 thành phần chính của Android, có nhiệm vụ quản lý sự kiện, chuyển tiếp sự kiện
trong hệ thống (vd: xử lý giao tiếp/ truyền thông giữa hệ điều hành và ứng dụng
Android)

• Why use Broadcast Receiver?


o Có thể nhận sự kiện từ hệ điều hành cả khi ứng dụng tắt.
• When to use Broadcast Receiver?
o Xử lý 1 công việc phụ thuộc vào sự kiến xuất hiện do HĐH thông báo (VD: lắng nghe
sự kiện mạng, tin nhắn, cuộc gọi tới….)
• How to use Broadcast Receiver?
Receive system events
Process
System events
https://developer.android.com/reference/android/content/Intent.html
Create your own broadcaster

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:

void onReceive(Context curContext, Intent broadcastMsg)

1.When a broadcast message arrives for the receiver, Android calls its
onReceive()method and passes it the Intent object containing the message.

2.The broadcast receiver is considered to be active only while it is executing


this method.

3.When onReceive() returns, it is inactive.

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

There are two major classes of broadcasts that can be received:

1.Normal broadcasts(sent with Context.sendBroadcast) are completely


asynchronous. All receivers of the broadcast are run in an undefined order,
often at the same time.

2.Ordered broadcasts(sent with Context.sendOrderedBroadcast) are


delivered to one receiver at a time.

The order receivers run in can be controlled with the android:priority


attribute of the matching intent-filter;

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

MySmsReceiver is as a services when register in Manifest XML.


When .apk is installed in the Phone, it becomes a Services. So you don’t need to launch
the application but it still receives broadcast when any SMS come in.

Automatic go to onReceive

SMS come in

22
Services

A Service is an application component that runs in the background, not


interacting with the user, for an indefinite period of time.

Run in the main thread of their hosting process. This means that

Each service class must have a corresponding <service> declaration in its


package's AndroidManifest.xml.

Services can be started with Context.startService() and


Context.bindService().

31
Services

singleton

Multiple calls to Context.startService() do not nest (though they do result in


multiple corresponding calls to the onStart() method of the Service class)

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.

stopped when: Context.stopService() or stopSelf() is called.

32
Services
 Service Life Cycle

Like activity
has lifecycle methods to monitor changes in its state.

fewer than the activity methods

1.void onCreate()

2.void onStart(Intentintent)

3.void onDestroy()

33
Services
 Service Life Cycle

The entire lifetime of a service happens between the time onCreate() is


called and the time onDestroy() returns.

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

The manifest of applications using Android Services must include:

1.A <service> entry for each service used in the application.

2.If the application defines a BroadcastReceiveras 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.

35
Services

36
Services Example 1:

37
Services Example 1:

38
Services

Communication with service

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

1.The main activity starts the service and registers a receiver.

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.

4.The intent is broadcasted using the filter: matos.action.GOSERVICE3.

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

sync from activity to service

Activity get service’s instance

Throught ServiceConnection (Activity) + Binder (Service)

Using bindService method

unbindService: for stop service

48
Services  Using Messager

Extends Handler for activity and service

Take Messenger for activity and service


Activity get Messenger from service in method onServiceConnected

Send message between activity and service from messenger


Messenger.Send(Message)

Process message in handleMessage(Message msg)


Define constant

49
https://developer.android.com/

50

You might also like