简体   繁体   English

警报管理器在特定时间设置每日警报?

[英]Alarm Manager set Daily Alarm at a specific time?

I've been working on this application that is supposed to run at a given time daily, except on weekends. 我一直在研究该应用程序,该应用程序应该每天在给定时间运行,周末除外。 I've used an AlarmBroadCastReceiver to fire a certain block of code at a given time. 我已经使用AlarmBroadCastReceiver在给定的时间触发特定的代码块。 I have this code in my AlarmBroadCastReceiver class: 我的AlarmBroadCastReceiver类中有以下代码:

public void SetAlarm(Context context) {
    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 2);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 0, pi);

}

Basically I try and set a repeating alarm on that time. 基本上,我尝试在该时间设置重复警报。

Then at a press of a button, I have this in my MainActivity where I'm calling it from: 然后按一下按钮,我在MainActivity中将其命名为:

public void onStartAlarmClicked(View view){
    Context context = this.getApplicationContext();
    if(alarm != null){
        Log.e(TAG, "starting alarm");
        alarm.SetAlarm(context);
    }else{
        Log.e(TAG, "Alarm is null");
    }
}

where alarm is an object of the AlarmBroadCastReceiver class. 其中alarmAlarmBroadCastReceiver类的对象。

The problem I have with it is that the code only fires once. 我的问题是代码仅触发一次。 Once it hits 2:30, it fires. 一旦达到2:30,它就会开火。 However, when I set the time back to 2:29 and wait for 2:30, or set the date 1 day forward and then set the time to 2:20 and wait for 2:30, the code no longer fires. 但是,当我将时间设置回2:29并等待2:30,或者将日期设置为1天前,然后将时间设置为2:20并等待2:30时,代码将不再触发。

I have a feeling I'm overlooking something rather simple with regards to setting the alarm time but I can't see it right now. 我觉得我在设置闹钟时间方面忽略了一些相当简单的事情,但是现在看不到。

Found a better answer from this answer. 从此答案中找到一个更好的答案。 Tweaked it a bit. 稍微调整一下。

public void Set12nnAlarm(Context context) {

    AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    intent.putExtra(ONE_TIME, Boolean.FALSE);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

    // every day at 9 am
    Calendar calendar = Calendar.getInstance();

    calendar.setTimeInMillis(System.currentTimeMillis());

    // if it's after or equal 9 am schedule for next day
    if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
        Log.e(TAG, "Alarm will schedule for next day!");
        calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
    }
    else{
        Log.e(TAG, "Alarm will schedule for today!");
    }
    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);


    am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
}

You have to setup a BroadcastReceiver to listen to time and date changes, and then reset the alarm. 您必须设置一个BroadcastReceiver来收听时间和日期的更改,然后重置警报。 As you want the receiver to be triggered at all times, it is better to set in the Manifest. 由于您希望始终触发接收器,因此最好在清单中进行设置。

Create class in own file 在自己的文件中创建类

public class DateTimeChangeReceiver extends BroadcastReceiver {
    @Override 
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(Intent.ACTION_TIME_CHANGED) ||
            action.equals(Intent.ACTION_TIMEZONE_CHANGED) ||
            action.equals(Intent.ACTION_DATE_CHANGED))
        { 
            resetAlarm(); 
        } 
    } 
} 

And in the Manifest 并在清单中

<receiver android:name=".DateTimeChangeReceiver ">
    <intent_filter>
            <action android:name="android.intent.action.TIMEZONE_CHANGED" />
            <action android:name="android.intent.action.TIME_SET" />
            <action android:name="android.intent.action.DATE_CHANGED" />
    </intent_filter>
</receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM