0% found this document useful (0 votes)
49 views26 pages

Unit4 RM Broadcast

1) Broadcasts are messages sent by the Android system and apps to notify receivers of events. They are sent via Intent objects containing event details. 2) There are two main types of broadcasts - system broadcasts for platform events and custom broadcasts sent between apps. 3) Broadcast receivers are app components that register to receive broadcasts. They are notified via Intents and perform actions in response. Receivers can be registered statically in the manifest or dynamically in code.

Uploaded by

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

Unit4 RM Broadcast

1) Broadcasts are messages sent by the Android system and apps to notify receivers of events. They are sent via Intent objects containing event details. 2) There are two main types of broadcasts - system broadcasts for platform events and custom broadcasts sent between apps. 3) Broadcast receivers are app components that register to receive broadcasts. They are notified via Intents and perform actions in response. Receivers can be registered statically in the manifest or dynamically in code.

Uploaded by

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

Unit 4: Broadcast Receiver

Rachana Mehta
Broadcast

Broadcasts are messages sent by Android system and other Android


apps, when an event of interest occurs.

Broadcasts are wrapped in an Intent object. This Intent object’s


contains the event details such as,
android.intent.action.HEADSET_PLUG, sent when a wired headset
is plugged or unplugged.

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

Android provides three ways for sending a broadcast:


● Ordered broadcast.
● Normal broadcast.
● Local broadcast.

6
Ordered Broadcast

● Ordered broadcast is delivered to one receiver at a time.


● To send a ordered broadcast, use the sendOrderedBroadcast()
method.
● Receivers can propagate result to the next receiver or even abort the
broadcast.
● Control the broadcast order with android:priority attribute in the
manifest file.
● Receivers with same priority run in arbitrary order.

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?

● Broadcast receivers are app components.


● They register for various system broadcast and or custom broadcast.
● They are notified (via an Intent):
○ By the system, when an system event occurs that your app is registered
for.
○ By another app, including your own if your app is registered for that
custom event.

11
Register your broadcast receiver

Broadcast receivers can be registered in two ways:


● Static receivers
○ Registered in your AndroidManifest.xml, also called as Manifest-declared
receivers.
● Dynamic receivers
○ Registered using app or activities' context in your Java files, also called
as Context-registered receivers.

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.

● Register the broadcast receiver and specify the intent-filters:


○ Statically, in the Manifest.
○ Dynamically, with registerReceiver().

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

public class CustomReceiver extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
// This method is called when the BroadcastReceiver
// is receiving an Intent broadcast.
throw new UnsupportedOperationException("Not yet implemented");
}
}
16
Implement onReceive()
Example implementation of onReceive() method which handles power
connected and disconnected.

@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

● Register your receiver in onCreate() or onResume().

// Register the receiver using the activity context.


this.registerReceiver(mReceiver, filter);

● Unregister in onDestroy() or onPause().

// Unregister the receiver


this.unregisterReceiver(mReceiver);

19
Register a Local broadcast receiver

Register local receivers dynamically, because static


registration in the manifest is not possible for a local
broadcasts.

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

● Restricting your broadcast is strongly recommended.


● An unrestricted broadcast can pose a security threat.
● For example: If your apps’ broadcast is not restricted and includes sensitive
information, an app that contains malware could register and receive your data.

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

To enforce a permission when sending a broadcast:


● Supply a non-null permission argument to sendBroadcast().
● Only receivers that request this permission using the <uses-permission> tag in their
AndroidManifest.xml file can receive the broadcast.

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.

● Refer to the shared code.

Rachana Mehta

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy