如何设置报警妥善火在固定的时间? [英] How to set an alarm to fire properly at fixed time?

查看:185
本文介绍了如何设置报警妥善火在固定的时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个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);

这是不是在执行 23:22小时

我在做什么错了?我注意到firstTime和c.getTimeInMillis()差别很大的大小和长度。当我使用firstTime,所以当设定为30秒,报警被执行以及

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.

推荐答案

正在使用 AlarmManager.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.

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

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).

您使用 AlarmManager.ELAPSED_REALTIME AlarmManager.ELAPSED_REALTIME_WAKEUP 当你通过指定的报警时间 SystemClock.elapsedRealtime()(因为手机启动毫秒)。

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).

这篇关于如何设置报警妥善火在固定的时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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