日期和时间在警报管理器中不起作用

编程入门 行业动态 更新时间:2024-10-26 11:18:26
本文介绍了日期和时间在警报管理器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用 AlarmManager 时遇到麻烦。我想在特定时间使用日历运行 AlarmManager 。 但是日历无法工作,并且 AlarmManager 始终运行,无论从日历中花费的时间如何。

I'm having trouble working with the AlarmManager. I want to run AlarmManager using Calendar at a specific time. But the calendar doesn't work and AlarmManager always runs regardless of the time taken from the calendar.

AlarmManager mAlarmManger = (AlarmManager) Objects.requireNonNull(activity).getSystemService(view.getContext().ALARM_SERVICE); Intent intent = new Intent(activity, MyReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.WEEK_OF_YEAR, Calendar.MONTH , Calendar.DAY_OF_MONTH, 11, 55, 0); calendar.set(Calendar.AM_PM, Calendar.AM); if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){ mAlarmManger.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); } else{ mAlarmManger.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent); }

AlarmManager应该每天上午11:55运行,但将尽快运行

AlarmManager should run at 11:55 AM everyDay, but will run as soon as the app opens.

推荐答案

Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.HOUR_OF_DAY, 11); calendar.set(Calendar.MINUTE, 30); calendar.set(Calendar.SECOND, 0); if (calendar.getTimeInMillis() > System.currentTimeMillis()) { Intent intent = new Intent(MainActivity.this, AlarmReceiver.class); pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager am = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE); am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent); //Repeat every 24 h }

尝试一下。.

更多推荐

日期和时间在警报管理器中不起作用

本文发布于:2023-11-27 09:36:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报   中不   日期   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!