0
我正在使用報警管理器和廣播接收器設置通知。我試圖使用警報管理器的setInexactRepeating方法,但它不會在API 19上方的確切時間上報警。如何設置每週使用setExact重複鬧鐘的日曆時間?
所以我建議使用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循環將運行52次,即每年52周。
它將設置每年每週的日期。但是我只能得到下週的日期,即如果今天是11日,我會把日期定爲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);
}
}
謝謝。
如何註冊新警報?@Doug Stevenson – user5881997
謝謝您的回答。 – user5881997