如何设置闹钟在固定时间正确触发?

发布于 2024-09-04 10:02:06 字数 829 浏览 9 评论 0原文

我有这段代码,

Calendar c = new GregorianCalendar();
        c.add(Calendar.DAY_OF_YEAR, 1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 22);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        // We want the alarm to go off 30 seconds from now.
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 30*1000;
        long a=c.getTimeInMillis();

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                c.getTimeInMillis(), 1*60*60*1000, sender);

它没有在 23:22h 执行,

我做错了什么?我注意到firstTime和c.getTimeInMillis()在大小和长度上有很大不同。当我使用firstTime时,所以当设置为30秒时,闹钟执行得很好。

I have this code

Calendar c = new GregorianCalendar();
        c.add(Calendar.DAY_OF_YEAR, 1);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 22);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);

        // We want the alarm to go off 30 seconds from now.
        long firstTime = SystemClock.elapsedRealtime();
        firstTime += 30*1000;
        long a=c.getTimeInMillis();

        // Schedule the alarm!
        AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                c.getTimeInMillis(), 1*60*60*1000, sender);

It is not executed at 23:22h

What I am doing wrong? I noticed firstTime and c.getTimeInMillis() differs a lot in size and length. When I use firstTime, so when set to 30 seconds, the alarm is executed well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-09-11 10:02:07

您正在使用 AlarmManager.ELAPSED_REALTIME_WAKEUP 标志,但您正在使用日历对象。这两件事不能同时发生。

您需要使用 AlarmManager.RTCAlarmManager.RTC_WAKEUP 如果您使用日历或日期对象指定闹钟时间(自 1970 年以来的毫秒数)。

当您通过 SystemClock.elapsedRealtime() 指定闹钟时间(自手机启动以来的毫秒数)时,您可以使用 AlarmManager.ELAPSED_REALTIMEAlarmManager.ELAPSED_REALTIME_WAKEUP

You are using the AlarmManager.ELAPSED_REALTIME_WAKEUP flag, but you are using a Calendar object. These two things don't go together.

You need to use AlarmManager.RTC or AlarmManager.RTC_WAKEUP if you are specifying the alarm time using a Calendar or Date object (milliseconds since 1970).

You use AlarmManager.ELAPSED_REALTIME or AlarmManager.ELAPSED_REALTIME_WAKEUP when you are specifying the alarm time via SystemClock.elapsedRealtime() (milliseconds since the phone booted).

梦行七里 2024-09-11 10:02:07

如果您只想为下一次出现 hh:mm 设置闹钟,我可以使用以下代码成功

Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, 22);
    cal.set(Calendar.MINUTE, 19);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    //check if we want to wake up tomorrow
    if (System.currentTimeMillis() > cal.getTimeInMillis()){
        cal.setTimeInMillis(cal.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
    }

I had success with the following code, if you only want to set the alarm for the next occurance of hh:mm

Calendar cal = new GregorianCalendar();
    cal.setTimeInMillis(System.currentTimeMillis());
    cal.set(Calendar.HOUR_OF_DAY, 22);
    cal.set(Calendar.MINUTE, 19);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    //check if we want to wake up tomorrow
    if (System.currentTimeMillis() > cal.getTimeInMillis()){
        cal.setTimeInMillis(cal.getTimeInMillis()+ 24*60*60*1000);// Okay, then tomorrow ...
    }
溺深海 2024-09-11 10:02:07

要让闹钟在 30 秒后响起,请使用

Calendar cal = Calendar.getInstance();

获取当前时间,然后

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+30000, sender); 

编辑:

我认为问题是 ELAPSED_REALTIME_WAKEUP。这告诉 AlarmManager 您给它的时间是基于系统启动以来的时间。从现在起 30 秒内就可以了,但如果您希望它基于实时,您应该使用 RTC 或 RTC_WAKEUP。有关这些类型的完整说明,请参阅 javadoc

To get the alarm to go off 30 seconds from now, use

Calendar cal = Calendar.getInstance();

to get the current time, and then

AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+30000, sender); 

Edit:

I think the problem is the ELAPSED_REALTIME_WAKEUP. This tells the AlarmManager that the time you are giving it is based on time since system startup. This is fine for 30 seconds from now, but if you want it to be based on real time you should use RTC, or RTC_WAKEUP. See javadoc for full explanation of those types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文