07.3 Broadcasts
07.3 Broadcasts
Background
Tasks
Lesson 7
● Broadcasts
● Send a custom broadcasts
● Broadcast receivers
● Implementing broadcast receivers
● Restricting the broadcasts
● Best practices
Types of broadcast:
● System broadcast.
● Custom broadcast.
Few examples:
● An Intent with action, ACTION_BOOT_COMPLETED is broadcasted
when the device boots.
● An Intent with action, ACTION_POWER_CONNECTED is
broadcasted when the device is connected to the external
power.
This work is licensed under a
Android Developer Broadcasts Creative Commons Attribution 4.0 7
Fundamentals V2 International License
Broadcast
Custom broadcasts
vs. Implicit Intents
LocalBroadcastManager.getInstance(this)
This work is licensed under a
Android Developer Broadcasts Creative Commons Attribution 4.0 13
.sendBroadcast(customBroadcastIntent);
Fundamentals V2 International License
Broadcast
Receivers
To add an intent-filter:
● To your AndroidManifest.xml file, use <intent-
filter> tag.
● To your Java file use the IntentFilter object.
This work is licensed under a
Android Developer Broadcasts Creative Commons Attribution 4.0 21
Fundamentals V2 International License
Subclass a broadcast receiver
LocalBroadcastManager.getInstance(this).registerReceiver
(mReceiver,
new IntentFilter(CustomReceiver.ACTION_CUSTOM_BROADCAST));
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mReceiver);
● BroadcastReceiver Reference
● Intents and Intent Filters Guide
● LocalBroadcastManager Reference
● Broadcasts overview