java – 在Android中设置重复星期几的闹钟

有人可以为设定重复几天的闹钟提供良好的逻辑吗?我每周使用报警
alarmCalendar.set(Calendar.HOUR,AlarmHrsInInt);
            alarmCalendar.set(Calendar.MINUTE,AlarmMinsInInt);
            alarmCalendar.set(Calendar.SECOND,0);
            alarmCalendar.set(Calendar.AM_PM,amorpm);

            Long alarmTime = alarmCalendar.getTimeInMillis();

Intent intent = new Intent(Alarm.this,AlarmReciever.class);
                intent.putExtra("keyValue",key);
                PendingIntent pi = PendingIntent.getBroadcast(Alarm.this,key,intent,PendingIntent.FLAG_UPDATE_CURRENT);
                am.setRepeating(AlarmManager.RTC_WAKEUP,alarmTime,7*1440*60000,pi);

报警触发时间和7天后自动触发.

但我的要求是我想选择日子而不是7天.

像星期一,星期二,星期四上午9:00这样的事件 – 报警应该自动触发.如何在setRepeating中执行此操作.

有人可以帮助我吗?

谢谢!

解决方法

这些问题与你想要的一样.这些答案将会有所帮助:

您只需指定启动日期,然后每7天重复一次.在给定问题的答案中指定的方法很少:

How can i get the repeat alarm for week days using alarm manager?

Android Notification on specific weekday goes off directly

how to repeat alarm week day on in android

更新:

在你的评论中你说

How to set the triggerAtMillis part in setRepeating. say for example today is Tuesday,I choose weekly Monday,Wednesday,Friday. – What do I put for Wednesday ?

我明白,如果今天是星期二,如何设置闹钟让我们说星期三重复,对吧?首先,您可以使用mulltiple id来分别设置每天的闹钟.

然后可以添加alarmCalendar.set(Calendar.DAY_OF_WEEK,week);符合您现有的代码.根据本周(从1-7起),当天重复.您可以将其作为参数传递给函数.喜欢:

setAlarm(2); //set the alarm for this day of the week

    public void setAlarm(int dayOfWeek) {
        // Add this day of the week line to your existing code
        alarmCalendar.set(Calendar.DAY_OF_WEEK,dayOfWeek);

        alarmCalendar.set(Calendar.HOUR,AlarmHrsInInt);
        alarmCalendar.set(Calendar.MINUTE,AlarmMinsInInt);
        alarmCalendar.set(Calendar.SECOND,0);
        alarmCalendar.set(Calendar.AM_PM,amorpm);

        Long alarmTime = alarmCalendar.getTimeInMillis();
        //Also change the time to 24 hours.
        am.setRepeating(AlarmManager.RTC_WAKEUP,24 * 60 * 60 * 1000,pi); 
}

我从上述问题中抽出了一个例子.希望现在更清楚.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...