Create a wake-up alarm that is triggered 10 seconds later

Source: Internet
Author: User

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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.