Use of Android Alarm (RPM)

Source: Internet
Author: User

Alarm is an independent method of triggering intent at a predetermined time.

Alarm are outside the scope of the application, so they can be used to trigger application events or actions even after the application is closed. In combination with broadcast receiver, they can become particularly powerful and can be set up alarm to launch applications or perform actions, and applications do not need to be open or active.

For example, you can use alarm to implement an alarm program, perform a normal network query, or schedule a time-consuming or cost-intensive operation at an "off-peak" schedule.

For timed operations that occur only during the application life cycle, the combination of the handler class with the timer and the thread class is a better option that allows Android to better control system resources.

The alarm in Android remains active when the device is in sleep mode, it can be set to wake the device, however, all alarm will be canceled when the device restarts.

The alarm operation is handled by Alarmmanager, and the system service can be obtained through Getsystemservice, as follows:
Alarmmanager alarms = (Alarmmanager) getsystemservice (Context.alarm_service);

To create a new alarm, use the Set method and specify a alarm type, the trigger time, and the intent to invoke when the alarm is triggered. If you set the alarm to happen in the past, then it will trigger immediately.

There are 4 types of alarm. Your choice will determine what the time value you pass in the Set method represents, whether it is a specific time or time lapse:

Rtc_wakeup
At the specified moment (when setting the alarm), wake the device to trigger the intent.

Rtc
Triggers the intent at an explicit time, but does not wake the device.

Elapsed_realtime
After booting from the device, if the elapsed time reaches the total time, the intent is triggered, but the device is not awakened. The elapsed time includes any time the device sleeps. Note that the calculation point for the time lapse is since it was last started.

Elapsed_realtime_wakeup
When the total elapsed time is reached after the device is booted, the device is awakened and intent is triggered if necessary.

The alarm creation process is shown in the following fragment:

Java code:

    1. int alarmtype = Alarmmanager.elapsed_realtime_wakeup;
    2. Long timeorlengthofwait = 10000;
    3. String alarm_action = "Alarm_action";
    4. Intent Intenttofire = new Intent (alarm_action);
    5. Pendingintent pendingintent = pendingintent.getbroadcast (this, 0, Intenttofire, 0);
    6. Alarms.set (Alarmtype, timeorlengthofwait, pendingintent);
Copy Code

When alarm arrives, the pendingintent you specify will be triggered. Set another alarm and use the same pendingintent to replace the previously existing alarm.

       Cancel a alarm, call Alarmmanager's Cancel method, and pass in the pendingintent that you no longer want to be triggered, as shown in the following code:
       alarms.cancel (pendingintent);

       The next code snippet, set two alarm, and then immediately cancel the first alarm. The first alarm explicitly sets the wake-up device at a specific time and sends the intent. The second is set to start from the device, and the elapsed time is 30 minutes after the time of arrival if the device does not wake it in the sleep state.

Java code:

    1. Alarmmanager alarms = (Alarmmanager) getsystemservice (Context.alarm_service);
    2. String my_rtc_alarm = "My_rtc_alarm";
    3. String alarm_action = "My_elapsed_alarm";
    4. Pendingintent rtcintent = pendingintent.getbroadcast (this, 0, new Intent (My_rtc_alarm), 1);
    5. Pendingintent elapsedintent = pendingintent.getbroadcast (this, 0, new Intent (alarm_action), 1);
    6. Date T = new Date ();
    7. T.settime (Java.lang.System.currentTimeMillis () + 60*1000*5);
    8. Alarms.set (Alarmmanager.rtc_wakeup, T.gettime (), rtcintent);
    9. Alarms.set (Alarmmanager.elapsed_realtime, 30*60*1000, elapsedintent);
    10. Alarms.cancel (rtcintent);
Copy Code

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.