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)
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
)