【问题标题】:Alarm manager Not triggering with correct time警报管理器未在正确的时间触发
【发布时间】:2014-12-02 15:39:35
【问题描述】:

我知道这是 SO 中最常见的线程,但我没有为我的问题找到任何答案。我想在特定时间触发动态广播接收器。为此,我使用以下代码

public void method1() {
    br = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent i) {
            doTASK();
        }
    };
    registerReceiver(br, new IntentFilter("com.XXXX.wakeup"));
    pi = PendingIntent.getBroadcast(this, 0,
            new Intent("com.xxxx.wakeup"), 0);
    am = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));

    setAlarmManagerObject();
}

这里有setAlarmManagerObject()方法

   public void setAlarmManagerObject() {
    Calendar cal = Calendar.getInstance();
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH);
    int day = cal.get(Calendar.DAY_OF_MONTH);
    cal.set(year, month, day
            , com.XXXX.Constants.AlarmHour
            , com.xxxx.Constants.AlarmMinute
            , com.xxxx.AlarmSecond);

    am.setRepeating(AlarmManager.ELAPSED_REALTIME, cal.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, pi);
}

我的问题是,当我使用 AlarmManager.RTC_WAKEUP 时,它会在设备关闭时唤醒。我不想在我的应用程序中使用此属性。所以我使用了AlarmManager.ELAPSED_REALTIME,但是使用ELAPSED_REALTIME,警报管理器没有触发。我希望我的警报管理器同时触发。如果设备处于关闭状态或处于睡眠状态,则警报管理器也会在设备唤醒后关闭。它应该在准确的时间触发。任何帮助将不胜感激。

【问题讨论】:

  • 我也在寻找解决方案,?
  • 我已经回答了。请检查。
  • ELAPSED_REALTIME :根据设备启动后的时间量触发挂起的意图,但不会唤醒设备。经过时间包括设备处于睡眠状态的任何时间。您可能想尝试使用 RTC 代替 RTC_WAKEUP。

标签: android alarmmanager


【解决方案1】:

你需要使用这样的东西:-

 alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP (or AlarmManager.ELAPSED_REALTIME), 
 SystemClock.elapsedRealtime() + Constants.DELAY_IN_MILLIS, 
 pendingIntent);

您可以计算 DELAY_IN_MILLIS,它是从当前系统时间开始的时间延迟,以毫秒为单位。

对于重复警报,您可以编写如下代码:-

 alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
 SystemClock.elapsedRealtime() + Constants.DELAY_IN_MILLIS, 
 Constants.ONE_DAY_IN_MILLIS, 
 pendingIntent);

【讨论】:

  • 您遇到了什么问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多