Alarmmanager, pendingintent the use of accessories home monitoring 10 minutes after the app cancellation service is started again

Source: Internet
Author: User

Alarmmanager, pendingintent use \ Accessory Home Monitor 10 minutes after the app cancellation service starts again

Use of Alarmmanager and Paddingintent \

\ Accessory Home Monitor 10 minutes after the app cancellation service starts again

The corresponding alarmmanage has a Alarmmanagerservie service program, the service program is really provide alarm service, it mainly maintains the application registration of the various types of alarm and timely set up the impending alarm to alarm device (in the system, the Linux implementation of the device named "/dev/alarm"), and has been monitoring the alarm device, and once an alarm is triggered or an alarm event occurs, the Alarmmanagerservie service will traverse the alarm list to find the appropriate registration alarm and issue a broadcast. The service program starts and initializes the alarm device (/DEV/ALARM) at system boot time by the system service program System_service. Of course, there is a layer of encapsulation between the Java layer's alarmmanagerservice and the Linux alarm driver interface, which is JNI.

Alarmmanager the application from the service, the application developer does not care about the specific service, but uses the service directly through the Alarmmanager. This may be the benefit of the customer/service model. Alarmmanager and Alarmmanagerservie are communicated through binder, and they are a many-to-one relationship.

In the Android system, Alarmmanage provides 5 interface 5 types of alarm services.

5 Interfaces:

  1. Cancels a registered alarm that matches a parameter
  2. void Cancel (Pendingintent operation)
  3. //Register a new alarm
  4. void set (int type, long triggerattime, pendingintent operation)
  5. //Register a repeating type of alarm
  6. void setrepeating (int type, long triggerattime, long interval, pendingintent operation)
  7. //Set the default time zone for the time zone setting system. Requires Android.permission.SET_TIME_ZONE permissions
  8. void Settimezone (String timeZone)
  9. void setinexactrepeating (int type, long triggerattime, long interval, pendingintent operation)
    Set an imprecise version of a repeating alarm, which is relatively energy-efficient (power-efficient) because the system may combine several similar alarms into one to execute, reducing the number of wake-up times of the device.
    Several built-in interval are:
    Interval_fifteen_minutes
    Interval_half_hour
    Interval_hour
    Interval_half_day
    Interval_day
    If you set it to day, you may have all the alarms in this date merged.

5 Alarm Types

  1. Public static final int elapsed_realtime
  2. This type of alarm does not wake the system when the system goes to sleep. Until the next time the system wakes up to pass it, the time spent in the alarm is relative to the time it starts, including the sleep time, which can be obtained by calling Systemclock.elapsedrealtime (). The system value is 3 (0x00000003).
  3. After the specified delay, the broadcast is sent, but the device is not awakened.
  4. Public static final int elapsed_realtime_wakeup
  5. Can wake the system, use the same elapsed_realtime, the system value is 2 (0x00000002).
  6. After the specified demo, send the broadcast and wake the device
    The time delay is to systemclock.elapsedrealtime () the system to start.
  7. Public static final int RTC
  8. This type of alarm does not wake the system when the system goes to sleep. It is not delivered until the next time the system wakes up, the time spent in the alarm is absolute, and the time spent is UTC time, which can be obtained by calling System.currenttimemillis (). The system value is 1 (0x00000001).
  9. The broadcast is sent at the specified moment, but the device is not awakened
  10. Public static final int rtc_wakeup
  11. //Can wake up the system using the same RTC type with a system value of 0 (0x00000000).
  12. At the specified moment, send the broadcast, and wake the device
  13. Public static final int power_off_wakeup
  14. //Can wake up the system, it is a shutdown alarm, that is, the device in the shutdown state can also wake the system, so we call it shutdown alarm. Using the same method as the RTC type, the system value is 4 (0x00000004).

Note An important parameter, pendingintent. This pendingintent can be said to be intent further encapsulation, he contains both the intent description and the execution of the Intent Act (this definition may be less stringent), if intent is likened to an order, Pendingintent more like a person to place an order, because it is responsible for sending orders, but also responsible for the processing of orders sent, such as after the successful delivery of goods to prepare acceptance orders, send failed to re-return is canceled orders and other operations. Developers can call

Getactivity (Context, int, Intent, int)

Getbroadcast (Context, int, Intent, int)

GetService (Context, int, Intent, int)

Three different ways to get an instance of pendingintent.

getbroadcast--the pendingintent obtained by this function will play a broadcast function, just like calling the Context.sendbroadcast () function. When the system through it to send a intent to use the form of broadcast, and in the intent will contain the corresponding intent receive object, of course, this object can be specified when creating pendingintent, or through the action and category descriptions let the system automatically find the behavior of the object processing.

    1. Intent Intent = new Intent (Alarmcontroller. This, oneshotalarm.  class);
    2. Pendingintent sender = Pendingintent.getbroadcast (Alarmcontroller. This, 0, intent, 0);

getactivity--the pendingintent obtained by this function can start the new activity directly, just like calling Context.startactivity (Intent) But it's worth noting that this new activity is no longer the activity of the current process. We must use Intent.flag_activity_new_task in the intent.

    1. The pendingintent to launch we activity if the user selects this notification
    2. Pendingintent contentintent = pendingintent.getactivity (this, 0, new Intent (This , Alarmservice. class), 0);

getservice--the pengdingintent obtained by this function can start the new service directly, just like calling Context.startservice ().

    1. Pendingintent Malarmsender = Pendingintent.getservice (alarmservice.  This, 0, new Intent (Alarmservice. This , Alarmservice_service.  Class), 0);

Send the specified broadcast after 5s

Java code
  1. Alarmmanager alarmmgr = (alarmmanager) getsystemservice (Context.alarm_service);
  2. Intent Intent = new Intent (Getapplicationcontext (), Alarmreceiver.   Class);
  3. int requestcode = 0;
  4. Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
  5. Requestcode, intent, pendingintent.flag_update_current);
  6. Send broadcast after 5 seconds, send only once
  7. int triggerattime = Systemclock.elapsedrealtime () + 5 * 1000;
  8. Alarmmgr.set (Alarmmanager.elapsed_realtime, Triggerattime, pendintent);


Send the specified broadcast after 5s, then repeat the broadcast every 10 seconds

Java code
  1. Alarmmanager alarmmgr = (alarmmanager) getsystemservice (Context.alarm_service);
  2. Intent Intent = new Intent (Getapplicationcontext (), Alarmreceiver.   Class);
  3. int requestcode = 0;
  4. Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
  5. Requestcode, intent, pendingintent.flag_update_current);
  6. Send the broadcast after 5 seconds, and then repeat the broadcast every 10 seconds. The radio was sent directly to Alarmreceiver.
  7. int triggerattime = Systemclock.elapsedrealtime () + 5 * 1000;
  8. int interval = ten * 1000;
  9. Alarmmgr.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Triggerattime, Interval, pendintent);


Cancel an alarm

Java code
    1. Alarmmanager alarmmgr = (alarmmanager) getsystemservice (Context.alarm_service);
    2. Intent Intent = new Intent (Getapplicationcontext (), Alarmreceiver.   Class);
    3. Pendingintent pendintent = Pendingintent.getbroadcast (Getapplicationcontext (),
    4. 0, intent, pendingintent.flag_update_current);
    5. Alarms that match the above intent (filterequals (intent)) will be canceled
    6. Alarmmgr.cancel (pendintent);

Alarmmanager, pendingintent the use of accessories home monitoring 10 minutes after the app cancellation service is started again

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.