android - Is there an easier way to use AlarmManager or to set real-time based alarms? -
it seems way time actions based on actual elapsed time (as opposed uptime, stops when device sleeps) alarmmanager.
is there an easy way "wallclock"-based delayed exectuion, example through open-source wrapper around alarmmanager?
for normal timing operations, can use handler, easy such simple task should be:
- implement handler callback (no registration necessary)
- instantiate handler
- call sendemptymessagedelayed or similar functions
- to clean set delays, call
removecallbacksandmessages(null)
however, handler supports uptime-based delays, not sufficient (e.g. if want check server new messages every 15 minutes).
if want those, seems have use alarmmanager, not comfortable:
- define action alarm
- create receiver (either creating dedicated receiver class , declaring manifest, or implementing interface, registering receiver using registerreciever, , unregistering when done)
- create intent action
- wrap said intent in pending intent, , store pending intent if want cancel alarm
- fetch alarm manager (this requires context)
- set alarm
- when want cancel alarm, cancel using stored pendingintent
- should decide have multiple intents or intents changing data, have save them clean alarm manager afterwards
for normal timing operations, can use handler
only foreground activity. other use of handler
long-term polling unreliable @ best, ignoring concerns regarding uptime calculations.
define action alarm
this not necessary. not idea.
create receiver (either creating dedicated receiver class , declaring manifest, or implementing interface, registering receiver using registerreciever, , unregistering when done)
if want event occur when not in foreground , alarm go off while device asleep (_wakeup
alarms), manifest-registered receiver required. if _wakeup
not needed, service suffice. if need in foreground , receive event in activity, use creatependingresult()
give pendingintent
trigger onactivityresult()
of activity. though, in latter case, make more sense use postdelayed()
on view
or handler
.
and store pending intent if want cancel alarm
if storing pendingintent
option, handler
need, , alarmmanager
unsuitable. cancel alarm, need equivalent pendingintent
(one underlying intent
objects match based on filterequals()
, pendingintent
operation [activity, service, broadcast] same).
when want cancel alarm, cancel using stored pendingintent
no, cancel creating equivalent pendingintent
.
should decide have multiple intents or intents changing data, have save them clean alarm manager afterwards
no, cancel them creating equivalent pendingintent
.
is there an easy way "wallclock"-based delayed exectuion, example through open-source wrapper around alarmmanager?
creating wrapper code covers 80% of work have taken less time did write question. not aware of dedicated library this, partly because there wouldn't it.
or, use scheduledexecutorservice
, wakelock
, short-term things. unsuitable "every 15 minutes" scenarios, keeps device awake of time.