如何在android中重复闹钟工作日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12507901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to repeat alarm week day on in android
提问by ckpatel
I want to get alarm on monday to friday only. my code is here
我只想在星期一到星期五收到警报。我的代码在这里
if (chk_weekday.isChecked()) {
int day = calNow.get(Calendar.DAY_OF_WEEK);
if (day == 2 || day == 3 || day == 4 || day == 5
|| day == 6) {
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), 1 * 60 * 60 * 1000,
pendingIntent);
}
Have a Idea.
有一个想法。
回答by ckpatel
please try this code. is successfully run in my apps
请试试这个代码。在我的应用程序中成功运行
if (chk_monday.isChecked()) {
forday(2);
} else if (chk_tuesday.isChecked()) {
forday(3);
} else if (chk_wednesday.isChecked()) {
forday(4);
} else if (chk_thursday.isChecked()) {
forday(5);
} else if (chk_friday.isChecked()) {
forday(6);
} else if (chk_sat.isChecked()) {
forday(7);
} else if (chk_sunday.isChecked()) {
forday(1);
}
public void forday(int week) {
calSet.set(Calendar.DAY_OF_WEEK, week);
calSet.set(Calendar.HOUR_OF_DAY, hour);
calSet.set(Calendar.MINUTE, minuts);
calSet.set(Calendar.SECOND, 0);
calSet.set(Calendar.MILLISECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
}
回答by Lucifer
From your question i believe you want to perform certain activity on daily basis except Saturday, Sunday. So your code is right but you declare it wrong way, make changes as follows and try
从您的问题中,我相信您希望除周六、周日外每天进行某些活动。所以你的代码是正确的,但你声明它错误的方式,按如下方式进行更改并尝试
declare alarm in OnCreate() method
在 OnCreate() 方法中声明警报
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(), 1 * 60 * 60 * 1000, pendingIntent);
Now your alarm is set to repeat daily, and you need to perform action daily except Sat,Sunday
现在您的闹钟设置为每天重复,除了周六、周日外,您需要每天执行操作
if (chkMonday.isChecked())
{
activityToPerform();
}
if (chkTuesday.isChecked())
{
activityToPerform();
}
if (chkWednesday.isChecked())
{
activityToPerform();
}
if (chkThrusday.isChecked())
{
activityToPerform();
}
if (chkFriday.isChecked())
{
activityToPerform();
}
if (chkSaturday.isChecked())
{
activityToPerform();
}
if (chkSunday.isChecked())
{
activityToPerform();
}
private void activityToPerform()
{
// your action code
}
回答by Pratik
One way is when the alarm notify is receive in broadcast then check the next day if its saturday then set the alarm for monday otherwise just create with adding 1 day.
一种方法是在广播中收到警报通知时,然后检查第二天是否是星期六,然后将警报设置为星期一,否则只需添加 1 天即可创建。
You need to set new alarm every time for this.
您每次都需要为此设置新的闹钟。