【问题标题】:Setting an alarm from inside another alarm using AlarmManager class not getting fired correctly使用 AlarmManager 类从另一个警报内部设置警报未正确触发
【发布时间】:2015-07-25 05:52:26
【问题描述】:

下面的代码来自我的 MainActivity onCreate() 方法,在该方法中我设置了一个警报来立即运行服务 ScheduleAlarm,并且只运行一次。 sPrefs 是 SharedPreference 对象,它只负责设置此警报一次。服务 ScheduleAlarm 是一个 IntentService,它被完美地触发了。

if(sPrefs.getBoolean("SettingAlarmForFirstTime",true)) {
        //Creating alarm for showing notifications.
        Calendar calendar = Calendar.getInstance();
        //calendar.setTimeInMillis(System.currentTimeMillis());
        //calendar.set(Calendar.HOUR_OF_DAY, 8);

        Intent intent = new Intent(MainActivity.this, ScheduleAlarms.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);//Use AlarmManager.INTERVAL_DAY instead of int number here.
        editor.putBoolean("SettingAlarmForFirstTime", false);
        editor.commit();
    }

现在这是我的 ScheduleAlarms 服务类。当此服务启动时,从我调用 setWeeklyAlarms() 方法的位置调用 onHandleIntent() 方法。现在我的问题是从这个方法内部,我想根据从 API 服务器调用获得的时间设置整整一周的 7 个警报。现在我什至无法完美地执行一个警报。我设置的警报设置为延迟 3 秒后启动,这将调用另一个名为 NotificationService 的服务,但警报会立即触发,而不是等待 3 秒。请分析并告诉我为什么会这样。

public class ScheduleAlarms extends IntentService {
private boolean notificationsEnabled = true;
private String notificationTimings = "";

public ScheduleAlarms() {
    super("ScheduleAlarms");
}

@Override
protected void onHandleIntent(Intent intent) {
    setWeeklyAlarm();
}

private void setWeeklyAlarm() {
    Intent i = new Intent(ScheduleAlarms.this, NotificationService.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getService(ScheduleAlarms.this, 10001, i, 0);
    AlarmManager alarmManager = (AlarmManager) ScheduleAlarms.this.getSystemService(Context.ALARM_SERVICE);

/* 设置此alarmManager 以在延迟3 分钟后触发警报,但此警报会立即触发*/

    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 3*60*1000, pendingIntent);


}

}

【问题讨论】:

标签: java android service alarmmanager


【解决方案1】:

更改以下行:

alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 3*60*1000, pendingIntent);

到:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis() + 3 * 60 * 1000, 3 * 60 * 1000, pendingIntent);

警报立即响起,因为

AlarmManager.ELAPSED_REALTIME_WAKEUP + 0, ...

所以,第一次,它在 0 秒后启动。要更改这一点,您需要在此处添加一些时间。

AlarmManager.ELAPSED_REALTIME_WAKEUP + time

而且,它应该是重复的,所以你需要使用setRepeating

注意:3 * 60 * 1000 == 3 minutes, not 3 seconds

【讨论】:

  • 感谢您的快速回复,正在查看。
  • 它工作.. 谢谢。我实际上想要实现的另一件事是,我想设置一周的警报,每天都有自己的时间,我通过调用服务器获得。服务器返回格式为“0,08:00;1,09:00;2,18:30;3,08:00;4,08:45;5,09:45;5,10:00 “ 在哪里 ;是分隔符和子字符串,例如第一个 0,08:00 表示 0->Sunday 08->Hour of the day 和 00->Minutes。现在我应该实现什么逻辑?
  • 我是否应该在 setWeeklyAlarm() 方法中设置 7 个警报,当每个警报被触发时,我再次调用此 ScheduleAlarm 服务并更新 7 个警报,或者是否有任何其他更好的解决方案来解决我想要实现的目标?
  • 我是否应该在 setWeeklyAlarm() 方法中设置 7 个警报:这是一个非常糟糕的主意。从服务器获取字符串,检查今天,假设它是星期天,然后获取第一部分并相应地处理它。
  • 您应该使用不同的方法来创建警报,只要某些条件为真,只需调用该方法即可。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多