Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Alarms

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

Android Developer Fundamentals V2

Alarms and
Schedulers

Lesson 8

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 1
License.
8.2 Alarms

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 2
License.
Contents

● What are Alarms


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

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 3
License.
What Are
Alarms

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 4
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 5
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 Creative
Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 6
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 7
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 8
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 9
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 Creative
Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 10
License.
Alarms Best
Practices

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 11
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 12
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 13
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 14
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 15
License.
AlarmManager

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 16
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 17
License.
Get an AlarmManager

AlarmManager alarmManager =
(AlarmManager) getSystemService(ALARM_SERVICE);

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 18
License.
Scheduling
Alarms

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 19
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 20
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 21
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 22
License.
setInexactRepeating()

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

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 23
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 Creative
Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 24
License.
More Alarm
Considerations

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 25
License.
Checking for an existing alarm

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

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 26
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 27
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 Creative
Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 28
License.
Cancel an alarm

● Call cancel() on the AlarmManager


○ pass in the PendingIntent.

alarmManager.cancel(alarmPendingIntent);

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 29
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 30
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 Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 31
License.
What's Next?

● Concept Chapter: 8.2 Alarms


● Practical: 8.2 The Alarm Manager

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarms Commons Attribution 4.0 International 32
License.
END

This work is licensed under a Creative


Android Developer Fundamentals V2 Alarm Manager Commons Attribution 4.0 International 33
License.

You might also like