Implementing a boot prompt using static registration

Never put time-consuming logic inside onReceive()! The broadcast receiver’s onReceive runs on the main thread, and the execution time limit is very short (around 10 seconds). Performing heavy operations will cause the system to throw an ANR (Application Not Responding) error, crashing your app.

Recommended approach: The receiver should only handle lightweight triggering, such as:

  1. Displaying a notification.
  2. Starting a background Service.
  3. Launching an Activity. Offload complex business logic to a Service for processing; do not put it directly inside onReceive.

Implementing a boot prompt using static registration

This section contrasts with the previous one on dynamic registration and explains static registration of broadcast receivers to receive a system broadcast and display a Toast message once the device has finished booting.

1. Core Differences: Dynamic vs. Static Registration

  • Dynamic Registration: Registered via registerReceiver in code, typically inside an Activity’s onCreate method. The app must be launched at least once before it can receive broadcasts; once the app is closed, it stops receiving them. Registration and unregistration can be controlled manually, offering more flexibility.
  • Static Registration: Registered using the <receiver> tag in AndroidManifest.xml. The app does not need to be launched beforehand; after the system boots, it automatically wakes up the receiver to listen for system broadcasts.

2. Complete Steps

① Create the Broadcast Receiver Class BootCompleteReceiver
public class BootCompleteReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context, "System boot completed", Toast.LENGTH_LONG).show();
    }
}
  • How to create: In Android Studio, right-click the package → New → Other → Broadcast Receiver.
  • Parameter descriptions:
    • Enabled: Whether to enable this receiver (check this box).
    • Exported: Whether to receive system broadcasts from outside the app (check this box, since we need to receive the system boot broadcast, which counts as an external broadcast).

Setting exported="false" in the manifest, as shown in some course materials, is incorrect. To receive system broadcasts, you must set exported="true".

② Add Permissions in AndroidManifest.xml
<!-- Permission to receive the boot completed broadcast -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
③ Statically Register the Receiver + Intent Filter Inside <application>
<receiver
    android:name=".BootCompleteReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <!-- Action for boot completion -->
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
</receiver>
  • intent-filter: An intent filter that declares which type of broadcast this receiver listens for. Here, it’s set to monitor the BOOT_COMPLETED system boot broadcast.

3. Important Notes

Never put time-consuming logic inside onReceive()! The broadcast receiver’s onReceive runs on the main thread, and the execution time limit is very short (around 10 seconds). Performing heavy operations will cause the system to throw an ANR (Application Not Responding) error, crashing your app.

Recommended approach: The receiver should only handle lightweight triggering, such as:

  1. Displaying a notification.
  2. Starting a background Service.
  3. Launching an Activity. Offload complex business logic to a Service for processing; do not put it directly inside onReceive.

Implementing a boot prompt using static registration

Leave a Reply

Your email address will not be published. Required fields are marked *