In my application, I will use Alarm to notify the user of an event, the problem is that on Android version 6.x- An
alarm is given after the same minutes of exact time.
This only happens when the device goes into sleep mode.
I tried to pull out the device in front of the alarm, but to no avail.
Can anyone help me? this is my code
Set alarms
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
if (minutesBefore > 0) {
beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
if (minutesBefore > 0) {
beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
}
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, alarmCalendar.getTimeInMillis(), pendingIntent);
if (minutesBefore > 0) {
beforeCalendar.add(Calendar.MINUTE, -minutesBefore);
alarmManager.set(AlarmManager.RTC_WAKEUP, beforeCalendar.getTimeInMillis(), beforePendingIntent);
}
}
Weaklock
when I receive an alarm via broadcast, I try to wake the device from dose mode with this code
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Alarm_WakeLock");
wl.acquire();
But the alarm is still delivered after the same minutes!
source
share