Create an alarm that is only activated once. You can use the set method to specify an alarm type, trigger time, and pending intent to be activated.
Four Alarm types:
● Rtc_wakeup: Wake up the device at a specified time and activate the pending intent
● RTC: activates pending at a specified time point, but does not wake up the device.
● Elapsed_realtime: the time after which the device starts to activate pending intent, but does not wake up the device
● Elapsed_realtime_wakeup: Wake up the device and activate pending intent after the device is started and after a specified time
1: private void setAlarm() {
2: /**
3: * listing 9-16: Create a wake-up alarm, which is triggered 10 seconds later.
4: */
5: // obtain an alarm manager reference
6: AlarmManager alarmManager =
7: (AlarmManager)getSystemService(Context.ALARM_SERVICE);
8:
9: // if the device is in sleep state, set alarm to wake up the device
10: int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
11:
12: // trigger the device after 10 seconds
13: long timeOrLengthofWait = 10000;
14:
15: // create a pending intent for broadcast and Operation
16: String ALARM_ACTION = "ALARM_ACTION";
17: Intent intentToFire = new Intent(ALARM_ACTION);
18: PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0,
19: intentToFire, 0);
20:
21: // set alarm set the alarm
22: alarmManager.set(alarmType, timeOrLengthofWait, alarmIntent);