Android: How to use AlarmManager

“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 … Read more

How to check if AlarmManager already has an alarm set?

Following up on the comment ron posted, here is the detailed solution. Let’s say you have registered a repeating alarm with a pending intent like this: Intent intent = new Intent(“com.my.package.MY_UNIQUE_ACTION”); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.MINUTE, 1); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60, … Read more

Alarm Manager Example

This is working code. It wakes CPU every 10 minutes until the phone turns off. Add to Manifest.xml: … <uses-permission android:name=”android.permission.WAKE_LOCK”></uses-permission> … <receiver android:process=”:remote” android:name=”.Alarm”></receiver> … Code in your class: package yourPackage; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.PowerManager; import android.widget.Toast; public class Alarm extends BroadcastReceiver { @Override public void … Read more