1

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

4

2 回答 2

1

你的方法是正确的。

您唯一需要添加的是将其保存在数据库或其他地方。每次用户设置内容并与您交互时AlarmManager,也将其放入文件中。

当你得到BOOT_COMPLETED,加载数据并设置所有以前的警报。

于 2013-11-07T22:21:12.943 回答
0

在您的应用程序清单中:

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

然后实现一个 BroadcastReceiver 来接收广播:

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

要在重新启动后设置警报,您可以从共享首选项或数据库中选择警报值。

于 2015-07-15T16:09:03.637 回答