Broadcast Receivers in Android
Broadcast Receivers are a key component in the Android framework that allow applications to receive and respond to broadcast announcements from other applications or from the system itself. Broadcasts can be sent by the Android system (e.g., when the device boots up, when the battery is low) or by applications (e.g., to communicate between different components within the same application or different applications).
Key Concepts
Broadcast Types:
- Normal Broadcasts: These are unordered and asynchronous. They are not prioritized, and all receivers run in an undefined order.
- Ordered Broadcasts: These are delivered to one receiver at a time. Receivers can propagate the broadcast to the next receiver or abort the broadcast.
- Sticky Broadcasts: These persist after they are sent, allowing future receivers to retrieve the data. Note: Sticky broadcasts are deprecated in API level 21 and above for security and memory concerns.
Declaring a Broadcast Receiver:
- Static Registration: Declared in the AndroidManifest.xml. The receiver is registered when the app is installed.
- Dynamic Registration: Registered at runtime using
Context.registerReceiver()
method. The receiver is active only while the application is running.
Implementation
Static Registration
Define the BroadcastReceiver:
public class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // Handle the broadcast } }
Declare in AndroidManifest.xml:
<receiver android:name=".MyBroadcastReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver>
Dynamic Registration
Define the BroadcastReceiver (same as above).
Register the Receiver in an Activity:
@Override protected void onStart() { super.onStart(); IntentFilter filter = new IntentFilter("android.intent.action.BOOT_COMPLETED"); MyBroadcastReceiver receiver = new MyBroadcastReceiver(); registerReceiver(receiver, filter); } @Override protected void onStop() { super.onStop(); unregisterReceiver(receiver); }
Sending Broadcasts
Normal Broadcast:
Intent intent = new Intent("com.example.MY_NOTIFICATION"); intent.putExtra("key", "value"); sendBroadcast(intent);
Ordered Broadcast:
Intent intent = new Intent("com.example.MY_NOTIFICATION"); intent.putExtra("key", "value"); sendOrderedBroadcast(intent, null);
Best Practices
- Avoid Overuse: Frequent broadcasts can affect system performance.
- Security Considerations: Be cautious about exposing broadcasts to other applications. Use permissions if necessary.
- Context Lifecycle: Always unregister dynamic receivers to prevent memory leaks.
- Broadcast Limits: Android 8.0 (API level 26) introduced limitations on background apps’ ability to register for implicit broadcasts.
Android Documentation Reference
For more detailed information and official guidelines, refer to the Android developer documentation on Broadcast Receivers:
By understanding and implementing Broadcast Receivers properly, you can create applications that effectively respond to system-wide or application-specific broadcasts, enhancing the interactivity and functionality of your applications.