Android Programming: The Big Nerd Ranch Guide, 2nd Edition
by Bill Phillips, Chris Stewart, Brian Hardy, Kristin Marsicano
Delayed Execution with AlarmManager
To actually use your service in the background, you will need some way to make things happen when none of your activities are running. Say, by making a timer that goes off every 5 minutes or so.
You could do this with a Handler by calling Handler.sendMessageDelayed(…) or Handler.postDelayed(…). But this solution will probably fail if the user navigates away from all your activities. The process will shut down, and your Handler messages will go kaput with it.
So instead of Handler, you will use AlarmManager. AlarmManager is a system service that can send Intents for you.
How do you tell AlarmManager what intents to send? You use a PendingIntent. You can use PendingIntent to package up a wish: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access