I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.
Can anyone help me with a basic alarm manager program?
I want to implement a schedule function in my project. So I Googled for an Alarm manager program but I can`t find any examples.
Can anyone help me with a basic alarm manager program?
Here's a fairly self-contained example. It turns a button red after 5sec.
Remember though that the AlarmManager fires even when your application is not running. If you call this function and hit the Home button, wait 5 sec, then go back into your app, the button will have turned red.
I don't know what kind of behavior you would get if your app isn't in memory at all, so be careful with what kind of state you try to preserve.
This code will help you to make a repeating alarm. The repeating time can set by you.
activity_main.xml
MainActivity.java
If you need alarm only for a single time then replace
with
I have made my own implementation to do this on the simplest way as possible.
The only next step, implement it.
I like to work with this implementation, but another possible good way, it's don't make the
AbstractSystemServiceTask
class abstract, and build it through a Builder.I hope it help you.
UPDATED Improved to allow several
BackgroundTaskListener
on the sameBroadCastReceiver
.This is working code. It wakes CPU every 10 minutes until the phone turns off.
Add to Manifest.xml:
Code in your class:
Set Alarm from Service:
If you want set alarm repeating at phone boot time:
Add permission and the service to Manifest.xml:
And create new class:
I think the best pattern for using
AlarmManager
is its collaboration with anIntentService
. TheIntentService
is triggered by theAlarmManager
and it handles the required actions through the receiving intent. This structure has not performance impact like usingBroadcastReceiver
. I have developed a sample code for this idea in kotlin which is available here:MyAlarmManager.kt
MyIntentService.kt
manifest.xml
Usage:
If you want to to cancel the scheduled alarm, try this:
I tried the solution from XXX and while it did initially work, at some point it stopped working. The
onReceive
never got called again. I spent hours trying to figure out what it could be. What I came to realize is that theIntent
for whatever mysterious reason was no longer being called. To get around this, I discovered that you really do need to specify an action for the receiver in the manifest. Example:Note that the name is
".Alarm"
with the period. In XXX'ssetAlarm
method, create theIntent
as follows:The
START_ALARM
message can be whatever you want it to be. I just gave it that name for demonstration purposes.I have not seen receivers defined in the manifest without an intent filter that specifies the action. Creating them the way XXX has specified it seems kind of bogus. By specifying the action name, Android will be forced to create an instance of the
BroadcastReceiver
using the class that corresponds to the action. If you rely upon context, be aware that Android has several different objects that are ALL called context and may not result in getting yourBroadcastReceiver
created. Forcing Android to create an instance of your class using only the action message is far better than relying upon some iffy context that may never work.