Search code examples
javaandroidandroid-intentandroid-activitybootcompleted

Start Activity on Reboot Only


Basically I am trying to make an activity containing a button which reboots the device and after the reboot returns to the same activity.

I understand that this question may get confused with others about rebooting the device, but that is not the focus of this question as I can get the device to reboot fine.

I have made the button reboot the device but the only way I can get it to start the activity after it's finished rebooting is to register a broadcast receiver for BOOT_COMPLETED in the manifest. The trouble is that this method starts the activity every time the device boots which is undesirable. When I register the receiver on the button click listener it does not start the activity after the reboot.

I was wondering if there might be an extra in BOOT_COMPLETED that I could use to decide if it had been purposefully rebooted.

Any advice would be appreciated, thanks in advance!


Solution

  • Just save an integer corresponding to device purposely being rebooted through your activity. Use SharedPreference for the same. On reboot, in your broadcast receiver, check if the value is set. If it is set, start your activity, otherwise, let it go.

    EDIT :

    Always, unset this value when reboot is complete and your Activity is in front.