For Marshmallow era(?), we need some ugly codes like below… 🙁
And “delayInMillis” param should be more than 15 minutes on the API 23.
If not, system ignore the minutes less than 15 minutes.
private void registerExactAlarm(PendingIntent sender, long delayInMillis) {
final int SDK_INT = Build.VERSION.SDK_INT;
AlarmManager am = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
long timeInMillis = (System.currentTimeMillis() + delayInMillis) / 1000 * 1000; //> example
if (SDK_INT < Build.VERSION_CODES.KITKAT) {
am.set(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
}
else if (Build.VERSION_CODES.KITKAT <= SDK_INT && SDK_INT < Build.VERSION_CODES.M) {
am.setExact(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
}
else if (SDK_INT >= Build.VERSION_CODES.M) {
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeInMillis, sender);
}
}