Alarm manager alarmmanager & pendingintent in Android

Source: Internet
Author: User
Tags time in milliseconds

Alarmmanager

1. the main function is to execute the specified task at the specified time. Note that all scheduled tasks will disappear after the mobile phone restarts. If you need to restart the task and continue to use it, you can add a boot auto-start function, then reset.
2. Usage: Set alarmmanager to send broadcasts at the specified time and write tasks in the receiver
Example:
Scheduled broadcast
Alarmmanager AM = (alarmmanager) getsystemservice (alarm_service );
Intent it = new intent (context, myrecevier. Class );
Pendingintent Pi = pendingintent. getbroadcast (this, 0, intent, 0 );
Am. Set (alarmmanager. rtc_wakeup, C. gettimeinmillis (), Pi );
Write the receiver class and register
<Cycler Android: Name = ". myrecevier" Android: Process = ". mycycler"/>
<! -- Android: The process name of the Process receiver. If this parameter is left blank, the package name is used by default. A user posted the following message on the Internet: "real machine test. If this parameter is left blank, when the alarmmanager time is set to the past time, it will not stop receiving broadcasts (endless loops) "-->
3. Methods and constants
Am. Set (alarmmanager. rtc_wakeup, C. gettimeinmillis (), Pi );
Set when the broadcast is sent. The first parameter indicates the alarm type, which can be:
Elapsed_realtime when the system goes to sleep state, this type of alarm will not wake up the system. It is not passed until the system is wake up next time. The time used for this alarm is relative time, starting from the start of the system, including the sleep time. You can call systemclock. elapsedrealtime. The system value is 3 (0x00000003 ).
Elapsed_realtime_wakeup can wake up the system in the same way as elapsed_realtime. The system value is 2 (0x00000002 ).
RTC does not wake up the system when the system is sleep. It is not passed until the system is wake up next time. The time used for this alarm is absolute time and UTC time. It can be obtained by calling system. currenttimemillis. The system value is 1 (0x00000001 ).
Rtc_wakeup can wake up the system, which is of the same type as RTC. The system value is 0 (0x00000000 ).
The first two use relative time, starting from the system startup, systemclock. elapsedrealtime () can obtain the current relative time in milliseconds, for example, alarmmanager. set (alarmmanager. elapsed_realtime_wakeup, systemclock. elapsedrealtime () + 5000, Sender );
The latter two use absolute time. The time takes 1970.1.1 as the reference time, system. currenttimemillis () obtains the time since January 1, in milliseconds, for example, alarmmanager. set (alarmmanager. rtc_wakeup, system. currenttimemillis () + 5000, Sender );

Am. setrepeating (alarmmanager. rtc_wakeup, C. gettimeinmillis (), 10000, Pi );
Based on the above method, the function of repeated execution is added. The second-to-last parameter is the interval, in milliseconds.
 
Am. Cancel (PI); // cancel the registered alarm that matches the Parameter
Public void cancel (pendingintent Operation)
Since: API Level 1 remove any alarms with a matching intent. Any alarm, of any type, whose intent matches this one (as defined by filterequals (intent), will be canceled.
 
Void settimezone (string timezone) // sets the time zone requires the permission Android. Permission. set_time_zone.
Void settime (long millis) // set the system wall clock time. requires the permission Android. Permission. set_time.
 
The pendingintent intent further encapsulates and adds the delayed execution function. The main difference between the two lies in the immediate execution of intent, while the execution of pendingintent is not immediate. pendingintent is an intent that can be executed under certain conditions. Compared with intent, pendingintent has its own context object, so that it does not have to rely on context to exist. The intent object contains the information required for the operation to be executed, and the pendingintent object contains the operation to be executed (send broadcast, start interface ,...)
You can obtain the pendingintent instance in three different ways.
Getbroadcast -- The pendingintent obtained through this function will act as a broadcaster, just like calling the context. sendbroadcast () function. When the system uses it to send an intent, it uses the broadcast format, and the intent contains the corresponding intent receiving object. Of course, this object can be specified when pendingintent is created, you can also use descriptions such as action and category to allow the system to automatically find the behavior processing object.
Java code
Intent intent = new intent (alarmcontroller. This, mycycler. Class );
Pendingintent sender = pendingintent. getbroadcast (alarmcontroller. This, 0, intent, 0 );

Getactivity -- The pendingintent obtained through this function can directly start a new activity, just like calling context. startactivity (intent) is the same. however, it is worth noting that this new activity is no longer an existing activity of the current process. Intent. flag_activity_new_task must be used in intent.
Java code
// The pendingintent to launch our activity if the user selects this notification
Pendingintent contentintent = pendingintent. getactivity (this, 0, new intent (this, alarmservice. Class), 0 );

Getservice -- The pengdingintent obtained through this function can directly start a new service, just like calling context. startservice.

// Create an intentsender that will launch our service, to be scheduled with the alarm manager.

Malarmsender = pendingintent. getservice (alarmservice. This, 0, new intent (alarmservice. This, alarmservice_service. Class), 0 );

This document references some of the online materials and makes modifications.

Related Article

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.