警报管理器在特定时间设置每日警报? [英] Alarm Manager set Daily Alarm at a specific time?

查看:107
本文介绍了警报管理器在特定时间设置每日警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究这个应用程序,该应用程序应该在每天的给定时间运行,周末除外.我已经使用AlarmBroadCastReceiver在给定的时间触发特定的代码块.我的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);

}

基本上,我尝试在该时间设置重复警报.

然后按一下按钮,我将其保存在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");
    }
}

其中,alarmAlarmBroadCastReceiver类的对象.

我遇到的问题是代码仅触发一次.一旦达到2:30,它就会开火.但是,当我将时间设置回2:29并等待2:30,或将日期设置为1天前,然后将时间设置为2:20并等待2:30时,代码将不再触发.

我觉得我在设置闹钟时间方面忽略了一些相当简单的事情,但是现在看不到.

解决方案

此答案中找到了一个更好的答案.进行了调整有点.

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);
}

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. I have this code in my AlarmBroadCastReceiver class:

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:

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.

The problem I have with it is that the code only fires once. Once it hits 2:30, it fires. 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.

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);
}

这篇关于警报管理器在特定时间设置每日警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆