为什么我的闹钟响直线距离? (机器人) [英] why does my alarm go off straight away? (android)

查看:130
本文介绍了为什么我的闹钟响直线距离? (机器人)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图设置为一周的某几天的报警,但目前困惑的是,为什么这个报警器是越来越马上还以颜色,不管是什么我通过在为小时和分钟......(我知道的东西,一周的日子错了,只是还没有到那种呢!)

Trying to set alarms for certain days of the week, but currently puzzled as to why this alarm is getting fired immediately, no matter what I pass in as hr and min... (I know the day of the week stuff is wrong, just haven't got to that yet!)

public void setReminder(int hr, int min, int day)
{
     Intent intent = new Intent(mContext, AlarmReceiver.class);

     PendingIntent sender = PendingIntent.getBroadcast(mContext, alarmId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

     Calendar calendar = Calendar.getInstance();
     calendar.set(Calendar.HOUR_OF_DAY, hr);
     calendar.set(Calendar.MINUTE, min);
     calendar.set(Calendar.SECOND, 0);
     calendar.setFirstDayOfWeek(Calendar.SUNDAY);
     calendar.set(Calendar.DAY_OF_WEEK, day);


     // set the alarm to repeat every week at the same time
     mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, sender);
}

什么想法?

推荐答案

如果日历 calendar.before(Calendar.getInstance())==真(这意味着你是设置报警,在过去的开始,它会立即触发。

If calendar calendar.before(Calendar.getInstance()) == true (it means your are setting alarm to start in the past, it will fire immediately.

在这种情况下,你可以做如下:

In such situation you can do following:

long start = calendar.getTimeMillis();
if (calendar.before(Calender.getInstance()) {
     start += AlarmManager.INTERVAL_DAY * 7;
}
// set alarm with start time

从<一个href=\"http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29\"相对=nofollow>文档:

如果在过去发生的时间,该报警器将被立即根据触发时间如何在过去远是相对于重复间隔触发,与报警计数。

If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.

这篇关于为什么我的闹钟响直线距离? (机器人)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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