我尝试在每天凌晨 12:00 触发闹钟,但为什么它在凌晨 12:00 的 10/20 分钟后触发?

I am try to trigger alarm for Every day 12:00 am but why it is trigger after 10/20 minute later of 12:00 am?

提问人:Md.Arifin Zaman 提问时间:10/27/2023 最后编辑:ChaosfireMd.Arifin Zaman 更新时间:10/27/2023 访问量:48

问:

   public void alarmUpdateForEveryDay(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context, NewDayReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 50, intent, PendingIntent.FLAG_IMMUTABLE);

        Calendar c = Calendar.getInstance();
        c.set(Calendar.HOUR_OF_DAY, 0); 
        c.set(Calendar.MINUTE, 0); 
        c.set(Calendar.SECOND, 0); 
        if (System.currentTimeMillis() > c.getTimeInMillis()) {
            c.add(Calendar.DAY_OF_MONTH, 1); // Add 1 day
        }

        long midnight = c.getTimeInMillis();

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, midnight, AlarmManager.INTERVAL_DAY, pendingIntent);
        
        Log.d("current_time", new Date(midnight).toString());
    }

我测试与调整系统时间

java android 函数 日历 重复闹钟

评论

0赞 Community 10/27/2023
请澄清您的具体问题或提供其他详细信息,以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。
0赞 Anonymous 10/27/2023
我建议你使用 java.time,现代 Java 日期和时间 API,用于你的日期和时间工作,忘记那些早已过时和麻烦的类。你需要类似的东西。CalendarDateLocalDate.now().plusDays(1).atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli()

答:

1赞 Biscuit 10/27/2023 #1

请查看文档说明

从 API 19 (Build.VERSION_CODES.KITKAT) 警报传递不准确:操作系统将转移警报,以最大限度地减少唤醒和电池使用。

应在应用中使用并创建重复机制setExact

评论

0赞 Md.Arifin Zaman 10/27/2023
感谢您的宝贵信息,,