首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在android中设置一周中的重复天数警报

在android中设置一周中的重复天数警报
EN

Stack Overflow用户
提问于 2013-07-27 03:53:52
回答 4查看 36.5K关注 0票数 19

有人能给出一个很好的逻辑来设定一周中重复几天的警报吗?我每周都会用

代码语言:javascript
运行
复制
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点一样-警报应该会自动触发。我该如何在setRepeating中这样做呢?

有人能帮我解决这个问题吗?

谢谢!

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-07-27 04:23:18

这些问题谈论的都是你想要的东西。这些答案将有帮助:

您只需要指定开始的日期,然后每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

更新:

你在评论中说

如何在triggerAtMillis中设置setRepeating部件。比方说今天是星期二,我选择每周星期一,星期三,星期五。-我星期三要放什么?

我所理解的是,如果今天是星期二,如何设置警报,让我们说星期三重复,对吗?首先,是的,您可以使用多个id来分别设置每天的警报。

然后,可以将alarmCalendar.set(Calendar.DAY_OF_WEEK, week);行添加到现有代码中。根据周一天(从1-7),它重复了那一天。您可以将它作为参数传递到函数中。比如:

代码语言:javascript
运行
复制
    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, alarmTime, 24 * 60 * 60 * 1000 , pi); 
}

我从上面的一个问题中拿出了这个例子。希望现在更清楚。

票数 18
EN

Stack Overflow用户

发布于 2018-07-30 05:47:28

若要设置一周的重复警报,请使用下面的代码。希望这会有帮助。

代码语言:javascript
运行
复制
        Calendar calender= Calendar.getInstance();

        calender.set(Calendar.DAY_OF_WEEK, weekNo);  //here pass week number
        calender.set(Calendar.HOUR_OF_DAY, hour);  //pass hour which you have select
        calender.set(Calendar.MINUTE, min);  //pass min which you have select
        calender.set(Calendar.SECOND, 0);
        calender.set(Calendar.MILLISECOND, 0);

        Calendar now = Calendar.getInstance();
        now.set(Calendar.SECOND, 0);
        now.set(Calendar.MILLISECOND, 0);

        if (calender.before(now)) {    //this condition is used for future reminder that means your reminder not fire for past time
            calender.add(Calendar.DATE, 7);
        }

        final int _id = (int) System.currentTimeMillis();  //this id is used to set multiple alarms

        Intent intent = new Intent(activity, YourServiceClass.class);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, _id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * 24 * 60 * 60 * 1000, pendingIntent);
票数 6
EN

Stack Overflow用户

发布于 2017-07-27 13:12:41

代码语言:javascript
运行
复制
if (Monday.isChecked()) {
                        setalarm(2);
                    } else if (Tuesday.isChecked()) {
                        setalarm(3);
                    } else if (Wednesday.isChecked()) {
                        setalarm(4);
                    } else if (Thursday.isChecked()) {
                        setalarm(5);
                    } else if (Friday.isChecked()) {
                        setalarm(6);
                    } else if (Saturday.isChecked()) {
                        setalarm(7);
                    } else if (Sunday.isChecked()) {
                        setalarm(1);
                    }

public void setalarm(int weekno) {

        cal.set(Calendar.DAY_OF_WEEK, weekno);
        cal.set(Calendar.MINUTE, min);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,


     cal.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17894067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档