Mohit Verma
Mohit Verma

Reputation: 3025

Alarmmanager.setRepeating triggers Immediately

Here is my code:

cal.set(Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH, hourOfDay, minute,Calendar.SECOND);

Intent intent=new Intent(FaceActivity.this,AlarmReceiver.class);
        PendingIntent pendingIntent=PendingIntent.getBroadcast(FaceActivity.this, 0, intent, 0);
        AlarmManager alarmManager=(AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

Here hourOfDay is one hour past the current hour. minute may have the same value.

Now here cal.getTimeInMillis() is set for time that is one hour past the current time. But when I run this code the broadcast receiver is called Immediately. Can any one tell, what am I doing wrong? Thanks in Advance...

Upvotes: 4

Views: 1864

Answers (1)

Mohit Verma
Mohit Verma

Reputation: 3025

Finally found my mistake, I was getting the wrong values for Year, Month, Day, Second.

By using below code, it works perfectly fine.

cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), hourOfDay, minute,cal.get(Calendar.SECOND));

Upvotes: 2

Related Questions