Other Interesting Articles
Examples of Using Intent in Apps
1. E-commerce Apps:
- Explicit Intent: Starts Activity product details when the user clicks on an item in the product list.
- Implicit Intent: Share product details to social media apps or emails.
2. Social Media Apps:
- Explicit Intent: Starts a comment Activity when the user clicks the “View Comments” button.
- Implicit Intent: Opens an external link in the user’s default browser.
3. Music Apps:
- Explicit Intent: Starts Service to play a song in the background.
- Implicit Intent: Share songs to other apps like WhatsApp or Instagram.
4. Broadcast Receiver: Responding to System Changes
Broadcast Receiver is an essential component in an Android app that allows your app to respond to Broadcast Intent sent by the Android system or other apps. Broadcast Intent is a system message that informs you about a change in status or a specific event, such as a change in battery status, network connectivity, or an incoming SMS message.
By using Broadcast Receiver, you can create apps that are more responsive to changes in the environment or system, thus providing a better user experience.
How Broadcast Receiver Works
Broadcast Receiver runs in the background and has no user interface. They are designed to respond to Broadcast Intent quickly and efficiently. Here are some key points about how Broadcast Receiver works:
1. Registration:
- Broadcast Receiver must be registered to the Android system to respond to Broadcast Intent.
- Registration can be done either static (via AndroidManifest.xml) or dynamic (via program code).
Example of static registration in AndroidManifest.xml:
<receiver android:name=“.MyBroadcastReceiver”>
<intent-filter>
<action android:name=“android.intent.action.BATTERY_LOW” />
</intent-filter>
</receiver>
Example of dynamic enrollment in code:
IntentFilter filter new IntentFilter(Intent.ACTION_BATTERY_LOW);
MyBroadcastReceiver receiver new MyBroadcastReceiver();
registerReceiver(receiver, filter);
2. Limited Execution Time:
- Broadcast Receiver only had about 5 seconds to complete the task.
- If a task takes longer, such as downloading data or processing files, we recommend that you start Service or use WorkManager.
3. Doable Tasks:
- Send Notifications: Notify users of status changes, such as low battery or internet connections.
- Start Service: Start Service to perform background tasks, such as data synchronization.
- Update Data: Update local data or send data to a server.
Types of Broadcast Intent
Broadcast Intent can be divided into two types:
1. Normal Broadcast:
- Sent asynchronously to all registered Broadcast Receiver.
- Example: ACTION_BOOT_COMPLETED (sent when the device finishes booting).
2. Ordered Broadcast:
- Sent sequentially to one Broadcast Receiver at a time.
- Broadcast Receiver can modify the results or stop the spread of Broadcast Intent.
- Example: ACTION_NEW_OUTGOING_CALL (sent when a user makes an outbound call).
Examples of Broadcast Receiver Usage
1. Responding to Battery Changes:
- Broadcast Receiver can detect when the device’s battery is low (ACTION_BATTERY_LOW) or is charging (ACTION_BATTERY_OKAY).
- Example: Sending a notification to the user when the battery is low.