本文介绍了日期和时间在警报管理器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在使用 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 }
尝试一下。.
更多推荐
日期和时间在警报管理器中不起作用
发布评论