Unit4 RM Broadcast
Unit4 RM Broadcast
Rachana Mehta
Broadcast
2
Types of broadcasts
Types of broadcast:
• System broadcast.
• Custom broadcast.
3
System broadcasts
System broadcast are the messages sent by the Android system, when a system
event occurs, that might affect your app.
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.
4
Custom broadcasts
Custom broadcasts are broadcasts that your app sends out, similar to the Android
system.
For example, when you want to let other app(s) know that some data has been
downloaded by your app, and its available for their use.
5
Send a custom broadcast
6
Ordered Broadcast
7
Normal Broadcast
● Delivered to all the registered receivers at the same time, in an undefined order.
● Most efficient way to send a broadcast.
● Receivers can’t propagate the results among themselves, and they can’t abort the broadcast.
● The sendBroadcast() method is used to send a normal broadcast.
8
Local Broadcast
● Sends broadcasts to receivers within your app.
● No security issues since no interprocess communication.
● To send a local broadcast:
○ To get an instance of LocalBroadcastManager.
○ Call sendBroadcast() on the instance.
LocalBroadcastManager.getInstance(this)
.sendBroadcast(customBroadcastIntent);
9
What is a broadcast receiver?
11
Register your broadcast receiver
12
Receiving a system broadcast
● Starting from Android 8.0 (API level 26), static receivers can't receive
most of the system broadcasts.
● Use a dynamic receiver to register for these broadcasts.
● If you register for the system broadcasts in the manifest, the Android
system won't deliver them to your app.
● A few broadcasts, are excepted from this restriction. See the complete list
of implicit broadcast exceptions.
13
To create a broadcast receiver
● Subclass the BroadcastReceiver class and override its onReceive() method.
14
What are Intent-filters
Intent-filters specify the types of intents a broadcast receiver can receive. They filter the
incoming intents based on the Intent values like action.
To add an intent-filter:
● To your AndroidManifest.xml file, use <intent-filter> tag.
● To your Java file use the IntentFilter object.
15
Subclass a broadcast receiver
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
switch (intentAction){
case Intent.ACTION_POWER_CONNECTED:
break;
case Intent.ACTION_POWER_DISCONNECTED:
break;
}
} 17
Register statically in Android manifest
● <receiver> element inside <application> tag.
● <intent-filter> registers receiver for specific intents.
<receiver
android:name=".CustomReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
18
Register dynamically
19
Register a Local broadcast receiver
20
Register a Local broadcast receiver
To register a receiver for local broadcasts:
● Get an instance of LocalBroadcastManager.
● Call registerReceiver().
LocalBroadcastManager.getInstance(this).registerReceiver
(mReceiver,
new IntentFilter(CustomReceiver.ACTION_CUSTOM_BROADCAST));
21
Unregister a Local broadcast receiver
To unregister a local broadcast receiver:
● Get an instance of the LocalBroadcastManager.
● Call LocalBroadcastManager.unregisterReceiver().
LocalBroadcastManager.getInstance(this)
.unregisterReceiver(mReceiver);
22
Restricting broadcasts
23
Ways to restrict a broadcast
● If possible, use a LocalBroadcastManager, which keeps the data inside your app,
avoiding security leaks.
● Use the setPackage() method and pass in the package name. Your broadcast is
restricted to apps that match the specified package name.
● Access permissions can be enforced by sender or receiver.
24
Enforce permissions by sender
25
Enforce permissions by receiver
To enforce a permission when receiving a broadcast:
● If you register your receiver dynamically, supply a non-null permission to
registerReceiver().
● If you register your receiver statically, use the android:permission attribute
inside the <receiver> tag in your AndroidManifest.xml.
26
Disclaimer
● All the content is curated from Android Documentation and Google Developer
Fundamentals.
Rachana Mehta