137
CHAPTER 8: APIs
Alarm Manager
If you need actions to happen at specific times, regardless of whether associated
components are running, the Alarm Manager is the system service that you can use for such
tasks.
As for matters concerning the Alarm Manager, your device is in one of the following states:
Device awake
The device is running. Usually this means also the screen is on, but
there is no guarantee that if the screen is off, the device is no longer
awake. Although often if the screen gets switched off, the device shortly
after that leaves the awake state. The details depend on the hardware
and the device’s software configuration. The Alarm Manager can do its
work if the device is awake, but being awake is not necessary for the
Alarm Manager to fire events.
Device locked
The device is locked, and the user needs to unlock it before it can be
handled again. A locked device might lead to the device going asleep;
however, locking per se is a security measure and has no primary
impact on the Alarm Manager’s functioning.
Device asleep
The screen is switched off, and the device runs in a low-power
consumption mode. Events triggered by the Alarm Manager will be
able to wake up the device and then fire events, but this needs to be
explicitly specified.
Device switched off
The Alarm Manager stops working and resumes working only the next
time the device is switched on. Alarm events get lost when the device is
switched off; there is nothing like a retry functionality here.
Alarm events are one of the following:
A PendingIntent gets fired. Since PendingIntents may target at either
services, activities, or broadcasts, an alarm event may start an activity
or a service or send a broadcast.
A handler gets invoked. This is a direct version of sending alarm events
to the same component that is issuing the alarms.
To schedule alarms, you first need to get the Alarm Manager as a system service as follows:
val alrm = getSystemService(AlarmManager::class.java)
// or, if API level below 23:
// val alrm = getSystemService(Context.ALARM_SERVICE)
// as AlarmManager
Do'stlaringiz bilan baham: |