【发布时间】:2016-02-11 09:14:35
【问题描述】:
我正在使用警报管理器和广播接收器设置通知。我曾尝试使用警报管理器的 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);
}
}
谢谢你..
【问题讨论】:
标签: java android datetime alarmmanager android-calendar