6

Hello I was reading on android docs about these two constans of AlarmManager but didn't get exactly the difference between them.

RTC Alarm time in System.currentTimeMillis() (wall clock time in UTC).

RTC_WAKEUP Alarm time in System.currentTimeMillis() (wall clock time in UTC), which will wake up the device when it goes off.

Does not RTC wake up the device and fire the PendingIntent when device is in sleeping mode ?

Thanks in advance.

Community
  • 1
  • 1
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • Possible duplicate of [Android AlarmManager - RTC\_WAKEUP vs ELAPSED\_REALTIME\_WAKEUP](http://stackoverflow.com/questions/5938213/android-alarmmanager-rtc-wakeup-vs-elapsed-realtime-wakeup) – Narkha Apr 24 '16 at 18:00

1 Answers1

16

Does not RTC wake up the device and fire the PendingIntent when device is in sleeping mode ?

RTC and ELAPSED_REALTIME do not wake up the device out of sleep mode. If the device is in sleep mode at the time of the event, nothing immediately happens. You will be notified about missed events when the device wakes up for other reasons (e.g., user presses the power button).

RTC_WAKEUP and ELAPSED_REALTIME_WAKEUP will wake up the device out of sleep mode. If your PendingIntent is a broadcast PendingIntent, Android will keep the device awake long enough for onReceive() to complete. If you have significant work, that you do not want to do in onReceive() (because onReceive() is called on the main application thread), you will need to arrange to keep the device awake long enough for some service of yours to complete the work, such as by using WakefulBroadcastReceiver.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • When I am running service from BroadcastReciever then Should I use `WakefulBroadcastReceiver` in those cases ? – N Sharma Feb 01 '15 at 12:46
  • @Williams: If you are using a `_WAKEUP`-style alarm, and you want the device to stay awake while your service processes some transaction, then yes. – CommonsWare Feb 01 '15 at 12:47
  • If I don't do then what will happen ? – N Sharma Feb 01 '15 at 12:47
  • @Williams: The device may fall back asleep before you finish your work. – CommonsWare Feb 01 '15 at 12:49
  • Yup, Suppose I am doing network operation there and device fall back asleep then Will this network operation fail or success ? – N Sharma Feb 01 '15 at 12:50
  • @Williams: In all likelihood, it will fail. – CommonsWare Feb 01 '15 at 12:54
  • ok thanks :). Suppose I have started a service from the activity and scheduled it every 4 hours using AlarmManager then How do I wake up and fallback back asleep device after code execution of service. I am not using here any WakefulBroadcastReceiver – N Sharma Feb 01 '15 at 12:57
  • @Williams: "How do I wake up and fallback back asleep device after code execution of service" -- use `AlarmManager`, a `WakefulBroadcastReceiver`, and an `IntentService`. – CommonsWare Feb 01 '15 at 12:58
  • So when I want to do any periodic network work in service then I should use `WakefulBroadcastReceiver` with `IntentService` not direct `IntentService` right ? – N Sharma Feb 01 '15 at 13:01
  • Thanks a lot for your help, your nice answers always helpful. :) +1 – N Sharma Feb 01 '15 at 13:02
  • The `WakefulBroadcastReceiver` is deprecated starting from Android 8. But we can use the simple `BroadcastReceiver` instead and schedule a job with `JobScheduler` or `WorkManager` in the `onReceive()` method. – Victor Semenovich Sep 20 '18 at 12:14