Android : Android AlarmManager is called even if time is passed

on Saturday, March 21, 2015


with the code below i set an alarm that will be called every thursday at 19:39:00. Everything works fine except the fact that if today its friday and i set the alarm, it will be called instantly and then goes on for every thursday normally.


So, how can i make the program skip the first time if it's already passed?



public void SetAlarm(Context context,int hours,int minutes,int seconds)
{
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);


Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);

calendar.set(Calendar.HOUR_OF_DAY, 19);
calendar.set(Calendar.MINUTE, 39);
calendar.set(Calendar.SECOND, 0);




PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);

//Set alarm every week. same day-time
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY*7, pi);
}

0 comments:

Post a Comment