Search code examples
androidbroadcastreceiveralarmreboot

Setting alarm after phone reboot


I have a problem. I am new to android and java and I'm making a program which turns off the phone sounds and dims the display at the user selected time. Everything is done by getting user input values (hour and minute) and setting the alarm based on these values to fire an intent to a BroadcastReceiver which starts a service which turns off sounds and dims the display. I want that alarm to be restarted after reboot. I thought that I can just simply set another BroadcastReceiver which receives BOOT_COMPLETED intent and then sets alarm which fires an intent to a BroadcastReceiver used before which starts the service. And my problem is that I don't know how to put the values from the activity which gets the user input to the BoradcastReceiver which is started by BOOT_COMPLETED intent. Or is there another way to set the same alarm based on the user input after reboot? In simply words I want to automatically set the alarm after reboot with the same fire time as the alarm which is set by the user. Sorry for my bad english...


Solution

  • Your approach is correct.

    The only thing you need to add is persisting it in DB or elsewhere. Every time user sets up stuff and you interact with AlarmManager, put it also in a file.

    When you get BOOT_COMPLETED, load data and set all previously alarms up.