MAD Chapter Five
MAD Chapter Five
Foreground Service
Normal Ordered
Broadcasts Broadcasts
Types of Broadcast Receiver
Ordered Broadcast:
• Ordered Broadcasts are synchronous broadcasts, and are
done in proper order.
• This order is decided by the android: priority attribute.
• The broadcast with the highest priority would execute
first and broadcasts with the same priority would not
follow any order.
• In this, one broadcast is delivered only to one receiver at
a time. When a receiver receives a broadcast it’s up to the
receiver to pass or abort the broadcast.
Types of Broadcast Receiver
Normal Broadcast:
• Normal broadcasts are asynchronous.
• This means all registered broadcast receivers receive the
broadcast at the same time, without any specific order.
• They are not interruptible, so receivers cannot stop the
broadcast or modify the content.
Broadcast Receiver Registration
Static Registration (manifest-declared):
• This receiver can be registered via the
AndroidManifest.xml file.
• This method is primarily used for system-wide broadcasts
or events that occur even when the app is not running.
• The receiver is always active as long as the app is installed.
It listens for broadcasts even when the app is not running
(e.g., boot completion).
Broadcast Receiver Registration
Dynamic Registration:
• Dynamic registration involves registering the Broadcast Receiver
programmatically at runtime.
• This method is used when you want the receiver to listen for
broadcasts only while the app or a specific component (like an
activity) is running.
• Receiver is active only when registered, typically during an activity or
service lifecycle.
• Must be explicitly unregistered to avoid memory leaks.
• Suitable for app-specific events like button clicks or temporary actions.
• Dynamic Registration is useful when you want the receiver to be
active only when your app is running, providing more control
over when it listens for broadcasts.
Broadcast Receiver Registration
Dynamic Registration:
• Dynamic registration involves registering the Broadcast Receiver
programmatically at runtime.
• This method is used when you want the receiver to listen for
broadcasts only while the app or a specific component (like an
activity) is running.
• Receiver is active only when registered, typically during an activity or
service lifecycle.
• Must be explicitly unregistered to avoid memory leaks.
• Suitable for app-specific events like button clicks or temporary actions.
• Dynamic Registration is useful when you want the receiver to be
active only when your app is running, providing more control
over when it listens for broadcasts.
How Broadcast Receiver Works?
Create a BroadcastReceiver:
• You must extend the BroadcastReceiver class and override
the onReceive() method to define what should happen
when a broadcast is received.
public class MyBroadcastReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// Handle the broadcast
Log.d("Receiver", "Broadcast received!");
}
}
How Broadcast Receiver Works?
Register the Receiver:
• You can register a BroadcastReceiver in two ways: statical
(via AndroidManifest.xml) or dynamically (via
registerReceiver() in code).
Static Registration
<receiver android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.MY_BROADCAST" />
</intent-filter>
</receiver>
Dynamic Registration
IntentFilter filter = new IntentFilter("com.example.MY_BROADCAST");
registerReceiver(myReceiver, filter);
Notification