androidbroadcastreceiveralarmbootcompleted

Broadcast intent android.intent.action.BOOT_COMPLETED received every single time I run / update the app. Why?


I have a very simple alarm app which needs to re-register it's alarms after phone reboots. However, seems like Android OS intent android.permission.RECEIVE_BOOT_COMPLETED does not work properly - I am receiving BOOT_COMPLETED every single time I update/deploy an app from Android Studio which is super odd and I believe incorrect behavior? This is my setup:

I declared a necessary permission in my AndroidManifest.xml:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

I declared my Broadcast receiver in my AndroidManifest.xml:

<receiver
    android:name=".MyReceiver"
    android:exported="true" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

I listen to intents like this:

public class MyReceiver extends BroadcastReceiver {
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().startsWith("android.intent.action.BOOT_COMPLETED")) {
            Log.i(this.TAG, "REBOOT"); // called every time I update an app
        }
    }
}

Anybody knows why such behavior happens? I can reproduce it on every device, every emulator, all the time.


Solution

  • This is likely due to behavior changes in Android 15. After force-stopping an app and restarting it, a BOOT_COMPLETED broadcast is sent to enable the app to re-initialize any PendingIntent, alarms, etc. since these are canceled on force stop:

    The intention of the package FLAG_STOPPED state (which users can engage in AOSP builds by long-pressing an app icon and selecting "Force Stop") has always been to keep apps in this state until the user explicitly removes the app from this state by directly launching the app or indirectly interacting with the app (through the sharesheet or a widget, selecting the app as live wallpaper, etc.). In Android 15, we've updated the behavior of the system to be aligned with this intended behavior. Apps should only be removed from the stopped state through direct or indirect user action.

    To support the intended behavior, in addition to the existing restrictions, the system also cancels all pending intents when the app enters the stopped state on a device running Android 15. When the user's actions remove the app from the stopped state, the ACTION_BOOT_COMPLETED broadcast is delivered to the app providing an opportunity to re-register any pending intents.

    Deploying from Android Studio will trigger this path by default, so this is actually working as intended. However, you can adjust your run configuration to disable this under the Miscellaneous tab by disabling: