2

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
        }
    }
}
  • Expected result: It should only happen after device reboot.
  • Actual Result: I see "REBOOT" logged every single time I deploy an app on my device from Android Studio.

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

2
  • instead of startsWith try using equals and not using a manually typed string instead use Intent.ACTION_BOOT_COMPLETED. using startsWith is weird since you are checking the whole action string anyway Commented Mar 25 at 12:15
  • true, I am aware of that. It's a legacy old code which I just copy pasted. Commented Mar 25 at 12:26

1 Answer 1

2

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:

  • Installation Options: Force stop running application before launching activity
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explanation. This was bugging me for a while.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.