【问题标题】:AlarmManager firing Alarm InstantlyAlarmManager 立即触发警报
【发布时间】:2013-05-21 03:25:23
【问题描述】:
Intent myIntent = new Intent(Alarm.this, Automaton.class);
                  myIntent.putExtra("methodName", "myMethod");
                  myIntent.putExtra("hour", tp.getCurrentHour().toString());
                  myIntent.putExtra("minute", tp.getCurrentMinute().toString());
                  hour = tp.getCurrentHour();
                  minute = tp.getCurrentMinute();
                  PendingIntent pendingIntent = PendingIntent.getActivity(Alarm.this, 0, myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
                  AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                       Calendar calendar = Calendar.getInstance();
                       calendar.set(calendar.HOUR_OF_DAY, hour);
                       calendar.set(calendar.MINUTE, minute);
                       calendar.set(calendar.SECOND, 0); 
                       alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 86400000, pendingIntent);

以上是我的代码。这是我的问题;我想在指定时间运行另一个 Activity 中的方法。如果用户选择了一天中还没有过去的时间(假设您将闹钟设置为上午 10 点到上午 11 点,它会正常工作)。但是,如果您将上午 9 点的闹钟设置为上午 10 点,它会立即触发 Pending Intent。不知道我哪里出错了。任何帮助将不胜感激!

【问题讨论】:

    标签: java android alarmmanager android-pendingintent


    【解决方案1】:

    您可能需要检查set time 是否小于current time。如果设置的时间小于当前时间,不要设置闹钟。因为过了时间再留也没用。

    【讨论】:

    • 感谢您的快速响应 Appu,假设我想现在(晚上 11:45)设置一个闹钟,明天早上 5:30 运行。从技术上讲,早上 5:30 已经过去了,所以如果我去设置它,它会立即触发。这就是我要避免的
    • 不,我可以说你理解事情的方式是错误的。因为警报应该在毫秒的帮助下设置。因此,从技术上讲,现在以毫秒为单位的晚上 11.45 小于明天上午 5:30(以毫秒为单位):)
    【解决方案2】:

    您可以使用日历实例并将日期设置为第二天并与闹钟一起使用。

    【讨论】:

      【解决方案3】:

      正如@shreyas 所说,如果你需要赶时间,你需要自己检查一下。

      示例.. 如果他们选择下午 5:00(HOUR_OF_DAY = 17),则将其与当前时间进行比较。如果该时间 (17) 小于当前时间,则在您的日历实例中添加一天。

      int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
      calendar.set(calendar.DAY_OF_WEEK, dayOfWeek + 1);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多