28
Scheduling Alarms Alexander Nelson September 23, 2019 University of Arkansas - Department of Computer Science and Computer Engineering

Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Scheduling Alarms

Alexander Nelson

September 23, 2019

University of Arkansas - Department of Computer Science and Computer Engineering

Page 2: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Reminders

Page 3: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Projects

Project 2 is out!

Due Friday October 6th Final Project Report 2 due Friday

Create a (temporary) App Name

Make your title be “AppName: Longer Description of Application”

Page 4: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

AlarmManager

Page 5: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarms

An alarm is used to wake the Android System and perform some

kind of operation

Operation is handled through the Alarm Manager

https://developer.android.com/reference/android/app/AlarmManager.html

These slides are modifications based on work created and shared by the

Android Open Source Project and used according to terms described in the

Creative Commons 2.5 Attribution License.

Page 6: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarms enable Active Behavior

Page 7: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Active Behavior

Passive transactions are initiated by the user

while

Active transactions are initiated by the system

Page 8: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Active Transactions

Active transactions can be defined by these properties:

1. Transaction initiated by system, user is given an opportunity

to respond

2. Require timely response from user

3. Interactions are sequential and serial

4. Between system and a single user

Example: Some 2-Factor authentication schemes

Page 9: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarm Operation

Alarms are scheduled for a future time, and are specified with an

Intent

When an alarm fires, the Intent is broadcast by the system

The Intent is created as a PendingIntent

• Grants the AlarmManager to run the Intent as if it was within

the Application itself

Intended for longer time requirements that operate outside the

lifetime of the application

Page 10: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarm Operation

When the Intent is broadcast by the system, the application is

automatically started if not already running

Registered alarms are retained until the device is restarted

Page 11: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarm Operation

The AlarmManager will hold a CPU wake lock while

AlarmReceiver’s onReceive() method is running

• Prevents the phone from sleeping until Broadcast is handled

• Releases wake lock after return

If the onReceive method tries to launch a service, the CPU may

sleep before the service Intent is handled

Page 12: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Using the Alarm Manager Class

Instances of the AlarmManager may be obtained by

• Context.getSystemService(AlarmManager.class) or

• Context.getSystemService(“Context.ALARM SERVICE”)

Page 13: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Alarm Manager Specifics

After API 19 (KitKat), alarm delivery is inexact

• Attempt to batch alarms to save battery

New APIs to support this functionality

setWindow() – Choose a window in which a wake-up call is

allowable

setExact() – Choose the specific time in which a wake up call will

occur

Page 14: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Best Practices

• Add randomness (jitter) to any network requests as a result ofrepeating alarms

• Do local work when alarm triggers

• Schedule the alarm that contains network requests to fire at a

random point in time

• Keep alarm frequency to a minimum

• Don’t wake device unnecessarily

• Don’t make alarms trigger time more precise than needed

• Avoid basing alarm on clock time if possible

• Doesn’t scale well (Can DDOS your own server)

• Exponential Backoff – When job fails, increase interval

between retries exponentially

Page 15: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

AlarmManager Functions

• set() – Older functionality, now Android chooses when to fire

• setAlarmClock() – Same, but implies Wake-Up and system

may show info to the user

• setWindow() – Inexact alarm, but with constraints about

wake-ups

• setExact() – Set exact alarm

• setRepeating() – Create exact repeating alarm (no longer

exact after API 19)

• setInexactRepeating() – Create inexact repeating alarm

• ...AndAllowWhileIdle() – Can be appended to several of these,

and allows wake-up during low-power modes

Page 16: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Examples

Example to create inexact repeating alarm to fire every 30 minutes

Will fire alarmIntent

Page 17: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Examples

Set inexact alarm (using old API call) for a one-time alarm to be

fired in 60 seconds

Page 18: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Real-Time Clock Example

Wake at approximately 2:00 PM, and once a day thereafter

Page 19: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Real-Time Clock Example

Wake at 8:30 AM, and every 20 minutes thereafter

Page 20: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Cancel an Alarm

There are some instances where you may wish to cancel an alarm

cancel() – Function within the AlarmManager class

Call on the instance of your AlarmManager, and pass the

pendingIntent to be cancelled

Page 21: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Starting Alarm on Device Boot

By default, all alarms are cancelled when the device shuts down

You can design your application to automatically restart an Alarm

when the device is rebooted

To do so, you must:

1. Set RECEIVE BOOT COMPLETED permission in manifest

2. Implement a BroadcastReceiver to handle boot broadcast

3. Add receiver into the application manifest file

Page 22: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Set Permissions

Set the permission in the manifest file

Page 23: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Register BroadcastReceiver

Register the BroadcastReceiver to handle the boot broadcast

Page 24: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Add Receiver to the application manifest

Create the Broadcast Receiver in the application manifest

<android:enabled=“false”>– Receiver will not be called unless

application explicitly enables it

Page 25: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Enable/Disable the BroadcastReciever

Enable the receiver programmatically, persists after reboots

Can disable by changing:

COMPONENT ENABLED STATE ENABLED to

COMPONENT ENABLED STATE DISABLED

Page 26: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Additional Resources

Android Framework JobScheduler

JobScheduler became available in API 21 (Lollipop)

Implemented in the platform, therefore can collect information

about all jobs needed run

Makes batching jobs easier

Recommended over AlarmManager for APIs 21 and above

Page 27: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Additional Resources

Firebase JobDispatcher

Open-source library that provides a similar interface to

JobScheduler

Provides compatibility for JobScheduler for apps targeting lower

than API 21

Page 28: Scheduling Alarms - University of Arkansasahnelson/CSCE4623/lectures/lecture9.pdf · Do local work when alarm triggers Schedule the alarm that contains network requests to re at a

Additional Resources

Sync Adapter – Designed specifically to sync data between device

and server

Android N and above – Implemented on top of JobScheduler

Services – You can always perform tasks running in the background