在android中每周重复一次闹钟 [英] Repeat Alarm once in a week in android
本文介绍了在android中每周重复一次闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的应用程序中开发闹钟功能,该应用程序在用户指定的工作日在固定时间运行.这里的问题是我的调度程序全天运行,而不是在指定的日期运行.这是我为此编写的代码,请帮助解决此问题
I am trying to develop alarm functionality in a my app which runs on a week days specified by user on fixed time. Problem here is that my scheduler running for all days instead of running on specified day . here is the code i wrote for this please help to fix this
Calendar calNow = Calendar.getInstance();
SimpleDateFormat simpDate;
simpDate = new SimpleDateFormat("kk:mm:ss");
if(in_Date==1)
{
calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
calNow.set(Calendar.MINUTE, minute);
calNow.set(Calendar.SECOND, 0);
calNow.set(Calendar.MILLISECOND, 0);
}
else if(in_Date==2)
{
calNow.set(Calendar.HOUR_OF_DAY, hourOfDay);
calNow.set(Calendar.MINUTE, minute);
calNow.set(Calendar.SECOND, 0);
calNow.set(Calendar.MILLISECOND, 0);
calNow.set(Calendar.DAY_OF_WEEK,in_SelectedDay);
}
etTime.setText(simpDate.format(calNow.getTime()));
Seconds=calNow.getTimeInMillis();
private void setAlarm(){
//etTime.setText(simpDate.format(calNow.getTime()));
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
if(in_Date==1)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,alarmManager.INTERVAL_DAY,pendingIntent);
}
else if(in_Date==2)
{
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds,1 * 60 * 60 * 1000,pendingIntent);
}
}
推荐答案
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY, pendingIntent);
在这一行中,您将开始时间设置为用户选择的日期,然后将间隔设置为 INTERVAL_DAY
.
In this line you set the start time to the user selected day, but then set the interval to INTERVAL_DAY
.
您应该使用 INTERVAL_DAY * 7
来确保它每周重复一次:
You should use INTERVAL_DAY * 7
to make sure it repeats on a weekly basis instead:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Seconds, AlarmManager.INTERVAL_DAY * 7, pendingIntent);
这篇关于在android中每周重复一次闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文