Question

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...

Was it helpful?

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.

OTHER TIPS

In your application's manifest:

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

Then implement a BroadcastReceiver to receive the broadcast:

public class SampleBootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
            // Set the alarm here.
        }
    }
}

For setting alarm after reboot you can pick values of your alarm from shared preferences or from DB.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top