Trouble with Android 6.0 Doze Alarm Manager

I created an application that always worked before Android 6.0. I think this is a Doze function that does not allow my alarm to fire.

I use sharedpreferences to handle the parameters:

//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 on what's wrong here?

This is a call blocking application. Thanks!

EDIT: I have 3 files (more, but ...), for example:

 MainActivity (All code) CallReceiver (Broadcast that triggers the alarm again (reboot etc)) CallReceiverService (Handles the call / phone state) 
+7
android android-pendingintent alarmmanager android-alarms
source share
2 answers

Dose mode delays the alarm until the next service window. To avoid Doze mode to block your alarm, you can use setAndAllowWhileIdle() , setExactAndAllowWhileIdle() or setAlarmClock() , You will have about 10 seconds to execute your code and set the next alarm (no more than once every 15 minutes for methods with _AndAllowWhileIdle though)

If you want to test Doze mode, you can use the ADB command :

  • Configure a hardware device or virtual device with Android 6.0 (API level 23) or a higher system image.

  • Connect the device to the development machine and install the application.

  • Launch the application and leave it active.
  • Turn off the device screen. (The application remains active.) Make the system cycle Doze modes by executing the following commands:

    adb shell dumpsys battery unplug

    adb shell dumpsys deviceidle step

  • You may need to execute the second command more than once. Repeat it until the device status changes to idle.

  • Observe the behavior of your application after turning on the device again. Make sure the application restores gracefully when the device exits Doze.

Edit: Add setAlarmClock example

Remember to check the SDK level ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP )

 AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, MyAlarmReceiver.class); //or just new Intent() for implicit intent //set action to know this come from the alarm clock intent.setAction("from.alarm.clock"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); //Alarm fire in 5s. am.setAlarmClock(new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + 5000, pi), pi); 
+13
source share

If the device is in dose mode, you need to use one of these APIs: setExactAndAllowWhileIdle or setAndAllowWhileIdle .

Please note that there is no API to wake the device in dose mode for a repeated alarm, therefore, if you need to repeat the alarm to wake the device during a dose, you must use the above APIs and reconnect the timer each time the timer starts.

+1
source share

All Articles