Android's Alarmmanager

Source: Internet
Author: User

In Android, the Alarm Manager service controls the alarm and wake-up functions. As with other system services, a secondary management class-alarmmanager is provided, and we only need to use Alarmmanager to invoke the Alarm manager service.

The following methods are available in Alarmmanager:

1. void Cancel (pendingintent operatioin): Cancels a registered timer

2, void set (int type,long triggerattime,pendingintent operation): sets a new timer.

3, void setinecactrepeating (int type,long triggeratmillis,long intervalmills,pendingintent operation): Set a timer for an imprecise repetition type.

4, void setrepeating (int type,long triggeratmills,long intervalmills,pendingintent operation): Set a repeating type of timer

5, void SetTime (Long Millis): Sets the system clock time.

6. Void Settimezone (String timeZone): Set time zone

In the method that sets the clock above, the first parameter sets the type of an alarm, and the following types are available in the system:

1. Elapsed_realtime: This type of alarm clock is not available in the system hibernation state and cannot wake the system. The time used is relative to the startup time of the system, which can be obtained by Systemclock.elapsedrealtime ().

2. Elapsed_realtime_wakeup: This type of alarm clock is available in system hibernation, and the time used is relative to the startup time of the system.

3, RTC: This type is not available in the system hibernation state, the use of real time.

4, Rtc_wakeup: This type is available in the system hibernation mode, using real time.

In the above method of setting the repeatable clock,intervalMillis参数的取值可以如下:

1, Interval_fifteen_minutes

2, Interval_half_hour

3, Interval_hour

4, Interval_half_day

5, Interval_day

How to use Alarmmanager, we explain it through a case.

 Public classAlarmmanageractivityextendsactionbaractivity {PrivateAlarmmanager Alarmmanager; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_alarm_manager); Alarmmanager=(Alarmmanager) Getsystemservice (Context.alarm_service); Intentfilter Filter=NewIntentfilter ("Com.jredu.action.MyAlarm");    Registerreceiver (Receiver,filter); }     Public voidSetClock (View v) {Intent I=NewIntent ("Com.jredu.action.MyAlarm"); Pendingintent Intent=Pendingintent.getbroadcast ( This, 0, i,pendingintent.flag_update_current); Alarmmanager.setrepeating (Alarmmanager.rtc_wakeup, System.currenttimemillis (),30*1000, intent); }     Public voidCancleclock (View v) {Intent I=NewIntent ("Com.jredu.action.MyAlarm"); Pendingintent Intent=Pendingintent.getbroadcast ( This, 0, i,pendingintent.flag_update_current);    Alarmmanager.cancel (Intent); }    PrivateBroadcastreceiver receiver =NewBroadcastreceiver () {@Override Public voidOnReceive (Context context, Intent Intent) {if(Intent.getaction (). Equals ("Com.jredu.action.MyAlarm") {toast.maketext (alarmmanageractivity). This, "This is the alarm I set!" ", Toast.length_long). Show ();    }        }    }; @Overrideprotected voidOnDestroy () {Super. OnDestroy ();    Unregisterreceiver (receiver); }}

Run as follows:

Jerry Education
Source:http://www.cnblogs.com/jerehedu/
Copyright Notice: The copyright of this article belongs to cigarettes DeskJerry EducationSectionTechnology Co., Ltd. and blog Park are shared, welcome reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
Technical Consultation:

Android's Alarmmanager

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.