3

i want to set an alarms every day of weeks at mighnight this is my code:

int notificationId = getNotificationId(); //it get a random number
Context context = rule.context;
Intent intent = ((Activity) context).getIntent();
long time = getRuleCalendar().getTimeInMillis();
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
     notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
     alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
     alarmManager.setExact(AlarmManager.RTC_WAKEUP, time,pendingIntent);
} else {
  alarmManager.set(AlarmManager.RTC_WAKEUP, time,pendingIntent);
}

public calendar getRuleCalendar(){
    Calendar calSet = Calendar.getInstance();
    calSet.set(Calendar.DAY_OF_WEEK, calendarDay); //calendarDay change by day of weeks
    calSet.set(Calendar.HOUR_OF_DAY, 0);
    calSet.set(Calendar.MINUTE, 0);
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 1);
    return calSet
}

now my problem is that one alarm start immediately (now is after mighnight and it is ok for me i want to check today) but all other alarms start at wrong time. why?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
fabio
  • 271
  • 5
  • 18
  • 1
    Read the [Documentation](https://developer.android.com/reference/android/app/AlarmManager.html) for alarm behavior. – ADM Dec 11 '17 at 14:19
  • Ok but where is the problem – fabio Dec 12 '17 at 00:15
  • You are setting a single alarm not repeating ? Am i right ? Then how you repeat it everyday of week ? Please explain then only i will be able to find a solution. – ADM Dec 12 '17 at 04:50
  • I want to set one alarm for day (7 alarm) at midnight with no repeat. first alarm start immediately and other alarms start on wrong time (today al 08:30). – fabio Dec 12 '17 at 20:46
  • What is calendarDay in getRuleCalender method . Post whole code – ADM Dec 13 '17 at 00:40

1 Answers1

2

The alarm will only fire immediately if you set the alarm in the past. For example in your case you are setting alarm for 00:00 AM for today which is alway be a past date for today. So you have set set the alarm for next day 00:00 AM . For that just add 1 in Calendar.DAY_OF_YEAR .

 public Calendar getRuleCalendar(){
    Calendar calSet = Calendar.getInstance();
    calSet.set(Calendar.HOUR_OF_DAY, 0);
    calSet.set(Calendar.MINUTE, 0);
    calSet.set(Calendar.SECOND, 0);
    calSet.add(Calendar.DAY_OF_YEAR,1);
    calSet.set(Calendar.MILLISECOND, 1);
    return calSet;
}

And for setting repeated alarm You have to check out the Documentation.

If you are not aware of background behavior change in Doze Mode starting from android M. Then have a look at Optimizing for Doze and App Standby.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Let me know if it works or if you need any help.Thx – ADM Dec 13 '17 at 04:44
  • i set alarms by add DAY_OF_YEAR to getRuleCalendar() method and set this alarms: – fabio Dec 13 '17 at 22:18
  • Mon Dec 18 00:00:00 GMT+01:00 2017 time:1513551600001 Tue Dec 12 00:00:00 GMT+01:00 2017 time:1513033200001 Wed Dec 13 00:00:00 GMT+01:00 2017 time:1513119600001 Thu Dec 14 00:00:00 GMT+01:00 2017 time:1513206000001 Fri Dec 15 00:00:00 GMT+01:00 2017 time:1513292400001 Sat Dec 16 00:00:00 GMT+01:00 2017 time:1513378800001 Sun Dec 17 00:00:00 GMT+01:00 2017 time:1513465200001 – fabio Dec 13 '17 at 22:18
  • i got only 1 alarm immediately and if i change data i do not have other alarm. for prevent doze situation i set alarm with setExactAndAllowWhileIdle() how you can see up. – fabio Dec 13 '17 at 22:24
  • **EDIT** if I reboot smathphone i have one alarm but why? – fabio Dec 13 '17 at 22:49
  • the reason you got only one alarm cause you are setting only one alarm. You are not using Set repeating, Read the documentation . And for handling reboot read [This](https://stackoverflow.com/questions/17941371/repeating-alarm-manager-after-reboot). – ADM Dec 14 '17 at 04:40