gpt4 book ai didi

android - 如何每天使用2次alarmmanager?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:42 27 4
gpt4 key购买 nike

我使用下面的代码每天只设置 alarmmanager 一次,我想设置 2 个不同的时间,所以每天 2 次。

我的代码:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 35);
calendar.set(Calendar.SECOND, 0); AlarmManager am = (AlarmManager) getApplicationContext().getSystemService (Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, new Intent(getApplicationContext(), AlarmReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

最佳答案

您可以使用 AlarmManager.INTERVAL_DAY/2 的间隔:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 35);
calendar.set(Calendar.SECOND, 0);
AlarmManager am = (AlarmManager)getApplicationContext().getSystemService (Context.ALARM_SERVICE);
Intent intent = new Intent(getApplicationContext(), AlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY / 2, pi);

但是如果你触发闹钟的时间很重要,你可以使用两个日历对象:

Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.HOUR_OF_DAY, 12); //midday
cal1.set(Calendar.MINUTE, 00);
cal1.set(Calendar.SECOND, 00);

Calendar cal2 = Calendar.getInstance();
cal2.set(Calendar.HOUR_OF_DAY, 18);//8pm for example
cal2.set(Calendar.MINUTE, 00);
cal2.set(Calendar.SECOND, 00);

并设置你的闹钟管理器:

am.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),cal2.getTimeInMillis(), pi);

关于android - 如何每天使用2次alarmmanager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13879321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com