abed
abed

Reputation: 35

android repeat alarm from next sunday in this year

my repeating alarm start first time at sunday regardless time (if past) here is the problem , in next sundays alarm repeat correctly , i want start alarm FROM NEXT SUNDAY .. this is my code :

 AlarmManager alarmMgr = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);

            Intent intent = new Intent(activity, Alarm.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(activity, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);


            Calendar calendarStart = Calendar.getInstance();

            calendarStart.set(Calendar.DAY_OF_WEEK, 1 );
            calendarStart.set(Calendar.HOUR_OF_DAY,8);
            calendarStart.set(Calendar.YEAR,calendarStart.get(Calendar.YEAR));              
            calendarStart.set(Calendar.MINUTE, 0);
            calendarStart.set(Calendar.SECOND, 0);

            Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();



            alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendarStart.getTimeInMillis() ,24*60*60*1000, pendingIntent);

Upvotes: 0

Views: 267

Answers (1)

Sam
Sam

Reputation: 86948

my repeating alarm start first time at sunday regardless time (if past)

Check if calendarStart is in the past and if it is add seven days:

if(calendarStart.compareTo(Calendar.getInstance()) < 0) { 
    // calendarStart is in the past, use next Sunday
    calendarStart.add(Calendar.DAY_OF_YEAR, 7);
}

Toast.makeText(activity, calendarStart.getTime()+"",Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions