0% found this document useful (0 votes)
67 views33 pages

Alarms and Schedulers: Lesson 8

Uploaded by

Tushar Vaze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views33 pages

Alarms and Schedulers: Lesson 8

Uploaded by

Tushar Vaze
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Android Developer Fundamentals V2

Alarms and
Schedulers

Lesson 8

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 1
national License
8.2 Alarms

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 2
national License
Contents

● What are Alarms


● Alarms Best Practices
● Alarm Manager
● Scheduling Alarms
● More Alarm Considerations

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 3
national License
What Are
Alarms

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 4
national License
What is an alarm in Android?

● Not an actual alarm clock.


● Schedules something to happen at a set time.
● Fire intents at set times or intervals.
● Goes off once or recurring.
● Can be based on a real-time clock or elapsed time.
● App does not need to run for alarm to be active.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 5
national License
How alarms work with components

BroadcastReceiver
BroadcastReceiver
wakes up the app
wakes up
and delivers the
delivers notification
notification.
Activity creates Alarm triggers and
a notification and sends out Intent.
sets an alarm.
App may be
destroyed so….
This work is licensed under a
Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 6
national License
Benefits of alarms

● App does not need to run for alarm to be active.


● Device does not have to be awake.
● Does not use resources until it goes off.
● Use with BroadcastReceiver to start services and other
operations.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 7
national License
Measuring time
● Elapsed Real Time—time since system boot.
○ Independent of time zone and locale.
○ Use for intervals and relative time.
○ Use whenever possible.
○ Elapsed time includes time device was asleep.

● Real Time Clock (RTC)—UTC (wall clock) time.


○ When time of day at locale matter.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 8
national License
Wakeup behavior

● Wakes up device CPU if screen is off.


○ Use only for time critical operations.
○ Can drain battery.

● Does not wake up device.


○ Fires next time device is awake.
○ Is polite.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 9
national License
Types of alarms
Elapsed Real Time Real Time Clock (RTC)—
(ERT)—since system time of day matters
boot
Do not wake ELAPSED_REALTIME RTC
up device
Wake up ELAPSED_REALTIME_W RTC_WAKEUP
AKEUP

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 10
national License
Alarms Best
Practices

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 11
national License
If everybody syncs at the same time...
Imagine an app with millions of users:
● Server sync operation based on clock time.
● Every instance of app syncs at 11:00 p.m.

Load on the server could result in high latency or even


"denial of service"

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 12
national License
Alarm Best Practices

● Add randomness to network requests on alarms.


● Minimize alarm frequency.
● Use ELAPSED_REALTIME, not clock time, if you can.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 13
national License
Battery
● Minimize waking up the device.
● Use inexact alarms.
○ Android synchronizes multiple inexact repeating alarms and
fires them at the same time.
○ Reduces the drain on the battery.
○ Use setInexactRepeating() instead of setRepeating().

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 14
national License
When not to use an alarm

● Ticks, timeouts, and while app is running—Handler.


● Server sync—SyncAdapter with Cloud Messaging Service.
● Inexact time and resource efficiency—JobScheduler.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 15
national License
AlarmManager

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 16
national License
What is AlarmManager

● AlarmManager provides access to system alarm services.


● Schedules future operation.
● When alarm goes off, registered Intent is broadcast.
● Alarms are retained while device is asleep.
● Firing alarms can wake device.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 17
national License
Get an AlarmManager

AlarmManager alarmManager =
(AlarmManager) getSystemService(ALARM_SERVICE);

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 18
national License
Scheduling
Alarms

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 19
national License
What you need to to schedule an alarm

1. Type of alarm.
2. Time to trigger.
3. Interval for repeating alarms.
4. PendingIntent to deliver at the specified time
(just like notifications).

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 20
national License
Schedule a single alarm

● set()—single, inexact alarm.


● setWindow()—single inexact alarm in window of time.
● setExact()—single exact alarm.

More power saving options AlarmManager API 23+.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 21
national License
Schedule a repeating alarm
● setInexactRepeating()
○ repeating, inexact alarm.
● setRepeating()
○ Prior to API 19, creates a repeating, exact alarm.
○ After API 19, same as setInexactRepeating().

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 22
national License
setInexactRepeating()

setInexactRepeating(
int alarmType,
long triggerAtMillis,
long intervalMillis,
PendingIntent operation)

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 23
national License
Create an inexact alarm
alarmManager.setInexactRepeating(
AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()
+ AlarmManager.INTERVAL_FIFTEEN_MINUTES,
AlarmManager.INTERVAL_FIFTEEN_MINUTES,
notifyPendingIntent);

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 24
national License
More Alarm
Considerations

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 25
national License
Checking for an existing alarm

boolean alarmExists =
(PendingIntent.getBroadcast(this,
0, notifyIntent,
PendingIntent.FLAG_NO_CREATE) != null);

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 26
national License
Doze and Standby

● Doze—completely stationary, unplugged, and idle device.


● Standby—unplugged device on idle apps.
● Alarms will not fire.
● API 23+.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 27
national License
User visible alarms
● setAlarmClock()
● System UI may display time/icon.
● Precise.
● Works when device is idle.
● App can retrieve next alarm with getNextAlarmClock().
● API 21+.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 28
national License
Cancel an alarm

● Call cancel() on the AlarmManager


○ pass in the PendingIntent.

alarmManager.cancel(alarmPendingIntent);

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 29
national License
Alarms and Reboots

● Alarms are cleared when device is off or rebooted.

● Use a BroadcastReceiver registered for the


BOOT_COMPLETED event and set the alarm in the
onReceive() method.

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 30
national License
Learn more

● Schedule Repeating Alarms Guide


● AlarmManager reference
● Choosing an Alarm Blog Post
● Scheduling Alarms Presentation
● Optimizing for Doze and Standby

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 31
national License
What's Next?

● Concept Chapter: 8.2 Alarms


● Practical: 8.2 The Alarm Manager

This work is licensed under a


Android Developer Fundamentals V2 Alarms Creative Commons Attribution 4.0 Inter 32
national License
END

This work is licensed under a


Android Developer Fundamentals V2 Alarm Manager Creative Commons Attribution 4.0 Inter 33
national License

You might also like