Skip to content

93 I need to trigger a block of code after 20 minutes from the AlarmManager being set. Can someone show me sample code on how to use an AlarmManager in ِAndroid? I have been playing around with some code for a few days and it just won't work. javaandroidalarmmanagerandroid-alarmsandroid-1.5-cupcake #62693

Closed Answered by Doraemon730
guymillicare asked this question in Mobile
Discussion options

You must be logged in to vote

"Some sample code" is not that easy when it comes to AlarmManager.

Here is a snippet showing the setup of AlarmManager:

AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent i=new Intent(context, OnAlarmReceiver.class);
PendingIntent pi=PendingIntent.getBroadcast(context, 0, i, 0);

mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), PERIOD, pi);
In this example, I am using setRepeating(). If you want a one-shot alarm, you would just use set(). Be sure to give the time for the alarm to start in the same time base as you use in the initial parameter to set(). In my example above, I am using AlarmManager.ELAPSED_REALTIME_WAKE…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by guymillicare
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Mobile
Labels
Mobile Triage your notifications and browse code on the go with the GitHub Mobile app Question
2 participants