2 つのカレンダーを比較しました: 年、月、日は現在の年、月、日です: しかし、最初のカレンダーの時刻が現在の時刻より後の場合、結果は期待どおりではありません。カレンダーはその時刻まで待機し、鳴り始めます。ただし、現在時刻より前の場合は、カレンダーが直接鳴り始めるため、常に 2 番目の条件にアクセスしています。
public void scheduleAlarm()
{
//to initialize the time variable not a real value:
Long time=Long.parseLong("0");
int hour=calendar.get(Calendar.HOUR);
int minute=calendar.get(Calendar.MINUTE);
Calendar cal_now = new GregorianCalendar(Year,Month,Day, hour, minute,Calendar.SECOND);
Calendar cal_alarm_first=new GregorianCalendar(Year,Month,Day,21,14,0);
//if the first calendar is in the past increment its day by one:
if(cal_alarm_first.before(cal_now)){
Notif_Body=getResources().getString(R.string.remembrance_body_first);
//here if the first alarm is already gone I should add a day and it should ring after a day from now with the specified hour and minute...
cal_alarm_first.add(Calendar.DATE, 1);
time=cal_alarm_first.getTimeInMillis();
}
else if(cal_alarm_first.after(cal_now)){
//the problem is here it always access this condition even if the first calendar time is before the current time for example same date but the first calendar is 9:14 and current time is 9:17 it always access this condition and the alarm starts ringing...
Notif_Body=getResources().getString(R.string.remembrance_body_first);
time=cal_alarm_first.getTimeInMillis();
}
//to send this alarm to be retrieved by the broadcast receiver class:
Intent intentAlarm = new Intent(this, TimeAlarm.class);
//add the notification body to the intent:
intentAlarm.putExtra("Notif_body", Notif_Body);
// create the object
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
//set the alarm for particular time
alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(this,1,intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
}
何が問題なのかさっぱりわかりません!! ありがとう。
編集: この予期しない結果の原因は、最初のカレンダーの時刻を 24 時間形式で追加している 24 時間形式のためであり、現在の時刻は 12 時間形式として配置されているため、両方を 24 時間で取得する必要がありますフォーマット;
Edit2: だから私は
int hour=calendar.get(Calendar.HOUR);//12 hours
に
int hour=calendar.get(Calendar.HOUROFDAY);//24 hours
そして基本的にうまくいった場合、それが本当にうまくいった場合は答えとして追加されます...