隔离日警报管理器 - 我想取消过去的警报



警报应每天8点至晚上8点重复。如何设置该警报不应在周日工作,并且过去的警报不应工作,即如果我直接打开应用程序,星期三,Mon-Tue的所有警报立即在屏幕上立即提示

int hour[]={8,9,10,11,12,13,14,15,16,17,18,19,20};

警报器AlarmManager;

  for (int i=0;i<hour.length;i++)
  {
      alarmManager= (AlarmManager) getSystemService(ALARM_SERVICE);
      Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
      intent.setAction(Intent.ACTION_MAIN);
      intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
      PendingIntent  pIntent = PendingIntent.getService(this, hour[i], intent, PendingIntent.FLAG_UPDATE_CURRENT);
      Calendar calendar = Calendar.getInstance();
      calendar.set(Calendar.MILLISECOND, 0);
      calendar.set(Calendar.SECOND, 0);
      calendar.set(Calendar.MINUTE, 0);
      calendar.set(Calendar.HOUR_OF_DAY, hour[i]);
      Log.d("sdf","dsgd g"+calendar.getTimeInMillis());
      alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pIntent);

对于您的周日问题,您可能只想询问一周中的一天是否不是7号(Java从1开始计数(。因此,在您的警报经理中:

Calendar c = Calendar.getInstance();
c.setTime(yourDate);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if(dayOfWeek != Calendar.SUNDAY){
 //fire alarm
}else{
 return
}

也是第二个问题:您应该询问该时间是否在当前时间之后,请设置警报。

  Calendar calendar = Calendar.getInstance();
  calendar.set(Calendar.MILLISECOND, 0);
  calendar.set(Calendar.SECOND, 0);
  calendar.set(Calendar.MINUTE, 0);
  calendar.set(Calendar.HOUR_OF_DAY, hour[i]);
  Calendar today = Calendar.getInstance();

要检查您的时间是否在当前时间之后,只需询问if(calendar.isAfter(today)){//set alarm}

相关内容

  • 没有找到相关文章

最新更新