Android Dose Mode

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);
        //Object flags;

            wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Alarm_WakeLock");

        wl.acquire();

But the alarm is still delivered after the same minutes!

0
source share
2 answers

, . , , AlarmManager.

, , [ setExactAndAllowWhileIdle()] (https://developer.android.com/reference/android/app/AlarmManager.html#setExactAndAllowWhileIdle(int, long, android.app.PendingIntent)).

docs :

, Doze, setAndAllowWhileIdle() setExactAndAllowWhileIdle().

+1

docs, , setExactAndAllowWhileIdle , , setAlarmClock.

, setAlarmClock(), - Doze , .

, . , - , ( ). , , .

https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/

0

Source: https://habr.com/ru/post/1681873/


All Articles