如何使用 setExact 设置每周重复闹钟的日历时间?
How to set Calendar time for repeating alarm every week using setExact?
我正在使用警报管理器和广播接收器设置通知。我尝试使用警报管理器的 setInexactRepeating 方法,但它不会在 API 19.
以上的确切时间发出警报
所以我得到了使用 setExact 方法并手动设置警报的建议。我不知道我该怎么做。我需要计算一年中每周的日期吗?
如何设置每周同一天的时间?就像如果我在本周的星期一设置了时间,我想为下周的每个星期一设置闹钟。
如何设置每周一天的时间?
我是这样设置闹钟的:
public void setNotificationTime(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
dateFrom.getTime();
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
if(notificationTime.equals("10 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
else if(notificationTime.equals("30 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 30);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
}
我的尝试是使用 for 循环来获取下周的日期并设置闹钟。所以 for 循环将 运行 52 次,即一年的 52 周。
它将为一年中的每个星期设置日期。但我只得到下周的日期,即如果今天是 2 月 11 日,我得到的日期是 2 月 18 日。它不会更进一步。这里出了什么问题?
public void setDates(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
dateFrom.getTime();
Date setDate = new Date();
SetDay(c);
Date changeDate = new Date();
for(int i=0;i<=52;i++) {
setDate.setTime(changeDate.getTime());
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day + 7);
// c.set(Calendar.DAY_OF_WEEK,);
// c.setTime(setDate);
notification = c.getTime();
notificationTime = df.format(notification);
changeDate.setTime(notification.getTime());
Log.i("changedate",String.valueOf(changeDate));
Log.i("dates",notificationTime);
}
}
谢谢..
请选择不精确的重复以帮助用户节省电量。这就是 Android 不希望您设置准确的重复警报的原因 -- 它会很快耗尽电池电量。
如果您必须精确重复,则必须设置一个精确警报,然后当它触发时,在您下次需要时注册另一个新的精确警报。根据需要重复此操作。
我正在使用警报管理器和广播接收器设置通知。我尝试使用警报管理器的 setInexactRepeating 方法,但它不会在 API 19.
以上的确切时间发出警报所以我得到了使用 setExact 方法并手动设置警报的建议。我不知道我该怎么做。我需要计算一年中每周的日期吗?
如何设置每周同一天的时间?就像如果我在本周的星期一设置了时间,我想为下周的每个星期一设置闹钟。
如何设置每周一天的时间?
我是这样设置闹钟的:
public void setNotificationTime(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
dateFrom.getTime();
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
if(notificationTime.equals("10 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
else if(notificationTime.equals("30 Minutes Before"))
{
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 30);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day);
// c.set(Calendar.DAY_OF_WEEK,);
SetDay(c);
notification = c.getTime();
notificationTime = df.format(notification);
Toast.makeText(getApplicationContext(),notificationTime,Toast.LENGTH_SHORT).show();
intent = new Intent(getBaseContext(),NotificationReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);
}
}
我的尝试是使用 for 循环来获取下周的日期并设置闹钟。所以 for 循环将 运行 52 次,即一年的 52 周。
它将为一年中的每个星期设置日期。但我只得到下周的日期,即如果今天是 2 月 11 日,我得到的日期是 2 月 18 日。它不会更进一步。这里出了什么问题?
public void setDates(Calendar c)
{
Date dateFrom = new Date();
df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
try {
dateFrom = df.parse(startTime);
}
catch (ParseException ex) {
}
c.setTime(dateFrom);
hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE);
dateFrom.getTime();
Date setDate = new Date();
SetDay(c);
Date changeDate = new Date();
for(int i=0;i<=52;i++) {
setDate.setTime(changeDate.getTime());
c.set(Calendar.HOUR_OF_DAY, hour);
c.set(Calendar.MINUTE, minute - 10);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
c.set(Calendar.DATE, day + 7);
// c.set(Calendar.DAY_OF_WEEK,);
// c.setTime(setDate);
notification = c.getTime();
notificationTime = df.format(notification);
changeDate.setTime(notification.getTime());
Log.i("changedate",String.valueOf(changeDate));
Log.i("dates",notificationTime);
}
}
谢谢..
请选择不精确的重复以帮助用户节省电量。这就是 Android 不希望您设置准确的重复警报的原因 -- 它会很快耗尽电池电量。
如果您必须精确重复,则必须设置一个精确警报,然后当它触发时,在您下次需要时注册另一个新的精确警报。根据需要重复此操作。