I've made an app that always worked until Android 6.0. I think it's the Doze feature that it's not allowing my Alarm to fire.
I use sharedpreferences to handle the options:
//ENABLE NIGHT MODE TIMER
int sHour = blockerTimerPreferences.getInt("sHour", 00);
int sMinute = blockerTimerPreferences.getInt("sMinute", 00);
Calendar sTime = Calendar.getInstance();
sTime.set(Calendar.HOUR_OF_DAY, sHour);
sTime.set(Calendar.MINUTE, sMinute);
Intent enableTimer = new Intent(context, CallReceiver.class);
enableTimer.putExtra("activate", true);
PendingIntent startingTimer = PendingIntent.getBroadcast(context, 11002233, enableTimer, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager sAlarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
sAlarm.setRepeating(AlarmManager.RTC_WAKEUP,
sTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, startingTimer);
Any clue of whats wrong here?
This is an app to block calls. Thank you!
EDIT: I have 3 files (more but...) like:
MainActivity (All code)
CallReceiver (Broadcast that triggers the alarm again (reboot etc))
CallReceiverService (Handles the call / phone state)
If the device is in doze mode, you need to use one of these API: setExactAndAllowWhileIdle or setAndAllowWhileIdle.
Note that there is no API for waking the device up while in doze mode for a repeating alarm, so if you need a repeating alarm to wake the device while in doze, you have to use the APIs above and re-arm the timer at every occurrence of the timer firing.
The Doze mode will delay your alarm until the next maintenance window. To avoid the Doze mode to block your alarm, you can use
setAndAllowWhileIdle()
,setExactAndAllowWhileIdle()
orsetAlarmClock()
. You will have about 10s to execute your code, and set your next alarm (not more than once per 15min for methods with_AndAllowWhileIdle
though)If you want to test the Doze mode, you can use ADB command:
Edit: Add setAlarmClock example
Don't forget to check the SDK level (
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP
)