8

I am using an AlarmManager in a Service to be triggered every minute.

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,
            getUpdateServiceIntent(mContext), PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);

    // Cancel any pending Intent
    am.cancel(pendingIntent);

    // Set a new one
    am.set(AlarmManager.RTC_WAKEUP, 60000, pendingIntent);

On the Samsung S5 Neo : When the screen is active, it is working as expected. When the screen is off, it is triggered every 5 minutes (instead of one).

I try this exact same code on S5 Mini (with Android 4.4), Nexus 5 5.1 and Nexus 5 6.0, this code is working fine.

targetSdkVersion is 19.

Any idea how to keep the AlarmManager working correctly when screen is off ? The delay is still 5 minutes, even if I ask for 30 seconds.

EDIT : I also tried the 'setExact' method, but it didn't change anything. Still have a 5 minutes interval between each alarm.

Mathieu H.
  • 800
  • 8
  • 21
  • I thought I would add some info to confirm answers below: https://developer.android.com/training/monitoring-device-state/doze-standby.html#restrictions – John Smith Oct 30 '16 at 17:23

1 Answers1

13

You should probably use

AlarmManager#setExact(int type, long triggerAtMillis, PendingIntent operation)

instead of

AlarmManager#set(int type, long triggerAtMillis, PendingIntent operation))

Take a look

From google :

AlarmManager#set(int type, long triggerAtMillis, PendingIntent operation))

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

Edit :

What i am using for an alarm application :

Manifest :

<uses-permission android:name="android.permission.WAKE_LOCK"/>

Java Code :

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(nextAlarm.getTimeInMillis(), pendingIntent);
  alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
}else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
  alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
}else {
  alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
}
Tim
  • 41,901
  • 18
  • 127
  • 145
Neige
  • 2,770
  • 2
  • 15
  • 21
  • 1
    I already tried the 'setExact' method but it didn't change anything : still 5 minutes to wait. I will edit my question to be more precise. – Mathieu H. Oct 21 '15 at 13:59
  • @MathieuH. Can you post permission from the manifest and which version of Android is the S5 Neo too? – Neige Oct 21 '15 at 14:08
  • I use many permission, including android.permission.WAKE_LOCK. S5 Neo android version is 5.1.1. – Mathieu H. Oct 21 '15 at 14:16
  • In fact your edit is working, but I have no idea why. Using AlarmClockInfo is working. Using setExact is not, even with RTC_WAKEUP. So thank you for your help ! – Mathieu H. Oct 21 '15 at 15:00
  • While the solution with setAlarmClock schedules alarm correctly on devices with issues with setExact it also changes next system alarm information (e.g. it will display time of the alarm on clock widgets). So it may be undesirable solution for non-alarm clock apps. – Paweł Nadolski Dec 02 '15 at 18:37
  • @Neige thank you, that was the only way to get my alarms working on my Samsung 5.1.1 devices (Galaxy S5 and Note 4). – user1256821 Mar 14 '16 at 09:30
  • Instead of making a user-visible AlarmClockInfo in the first if statement, you should be able to use setExactAndAllowWhileIdle() with identical results. – Egg Mar 31 '16 at 16:24
  • @Egg true for new api (23) – Neige Mar 31 '16 at 16:28
  • I'm tried it but not working in my phone i have GIONEE M5 lite with Lollipop (5.1) – Madhav_nimavat Jan 11 '17 at 05:58