Timer alarmmanager for Android

Source: Internet
Author: User

Finally:

When we click schedule, a time selector will pop up. After the time is selected, the system can schedule the timer. Note that the alarm is not implemented here. We didn't add the alarm code in the scheduled task, but simply printed it in logcat. This example focuses on the use of alarmmanager in Android.

Activity Code:

Package cn.com. chenzheng_java; </P> <p> Import Java. util. calendar; </P> <p> Import android. app. activity; <br/> Import android. app. alarmmanager; <br/> Import android. app. pendingintent; <br/> Import android. app. timepickerdialog; <br/> Import android. app. timepickerdialog. ontimesetlistener; <br/> Import android. content. intent; <br/> Import android. OS. bundle; <br/> Import android. util. log; <br/> Import android. view. V Iew; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. checkbox; <br/> Import android. widget. compoundbutton; <br/> Import android. widget. timepicker; <br/> Import android. widget. compoundbutton. oncheckedchangelistener; </P> <p> public class alarmmanageractivity extends activity implements onclicklistener, ontimesetlistener, oncheckedchangelistener {< Br/> calendar = calendar. getinstance (); // calendar representing the current time <br/> button button1; <br/> checkbox; <br/> Boolean flag = false; // whether to execute only once </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. alarm); </P> <p> button1 = (button) findviewbyid (R. id. button_alarm1); <br/> checkbox = (checkbox) findviewbyid (R. ID. Checkboxexternalarm); <br/> checkbox. setoncheckedchangelistener (this); </P> <p> button1.setonclicklistener (this ); <br/>}</P> <p> @ override <br/> Public void onclick (view v) {<br/> If (V = button1) {<br/> calendar. settimeinmillis (system. currenttimemillis (); <br/> New timepickerdialog (alarmmanageractivity. this, this, <br/> calendar. get (calendar. hour_of_day), <br/> calendar. get (calendar. minute), true ). show (); <br/>} <Br/>}</P> <p>/** <br/> * triggered when a new time is set. <Br/> */<br/> @ override <br/> Public void ontimeset (timepicker view, int hourofday, int minute) {</P> <p> // set the time to the scheduled time <br/> calendar. set (calendar. hour_of_day, hourofday); <br/> calendar. set (calendar. minute, minute); </P> <p> intent = new intent (alarmmanageractivity. this, myalarmbroadcast. class); <br/> pendingintent = pendingintent. getbroadcast (getapplicationcontext (), 0, intent, 0); </P> <p>/*** <br/> * obtain the global timer Service Manager <br/> */<br/> alarmmanager = (alarmmanager) getsystemservice (alarm_service); </P> <p> If (FLAG) {</P> <p>/** <br/> * The specified task is executed only once, if the task specified by the pendingintent has been executed, the method is directly canceled by cancel. <Br/> * Set (INT type, long triggerattime, pendingintent operation) <br/> * type specifies the scheduled mode. <Br/> * triggerattime indicates the time when the task is triggered. This parameter is closely related to the scheduled mode. <br/> * operation: this parameter specifies a broadcast intent. When the time reaches, the System Broadcasts the intent and triggers the corresponding broadcast receiver to perform some operations, for example, the bell ...... <Br/> */<br/> alarmmanager. set (alarmmanager. rtc_wakeup, calendar. gettimeinmillis (), pendingintent ); <br/>}< br/> else <br/> {<br/>/** <br/> * tasks specified by this method are executed at intervals, the third parameter specifies the execution interval <br/> * if you want to cancel the operation, use alarmmanager. cancel (pendingintent); <br/> * Note that the pendingintent here must be consistent with the setrepeating method. <Br/> */<br/> alarmmanager. setrepeating (alarmmanager. rtc_wakeup, calendar. gettimeinmillis (), 5*1000, pendingintent); <br/>}< br/> log. I ("notification", "scheduled successfully! "); </P> <p >}</P> <p> @ override <br/> Public void oncheckedchanged (compoundbutton buttonview, Boolean ischecked) {<br/> flag =! Ischecked; <br/>}</P> <p >}< br/>

Alarm. xml

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" Android: layout_height = "match_parent" <br/> Android: orientation = "vertical"> <br/> <analogclock Android: Id = "@ + ID/analogclock1_alarm" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </analogclock> <br/> <checkbox Android: text = "whether to repeat the bell" Android: Id = "@ + ID/checkbox1_alarm" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </checkbox> <br/> <button Android: text = "timed" Android: id = "@ + ID/button_alarm1" <br/> Android: layout_width = "wrap_content" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>

Myalarmbroadcast. Java

Package cn.com. chenzheng_java; </P> <p> Import android. content. broadcastreceiver; <br/> Import android. content. context; <br/> Import android. content. intent; <br/> Import android. util. log; <br/> Import android. widget. toast; </P> <p>/** <br/> * accept the broadcast published when alarmmanager time arrives. <br/> * @ author chenzheng_java <br/> */<br /> public class myalarmbroadcast extends broadcastreceiver {</P> <p> @ override <br/> Public void onreceive (context, intent intent) {<br/> log. I ("notification", "broadcast received "); <br/> // code where you can add a bell <br/>}</P> <p >}< br/>

Manifest. XML code:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "cn.com. chenzheng_java "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <uses-SDK Android: minsdkversion = "8"/> </P> <p> <application Android: icon = "@ drawable/icon" Android: label = "@ string/app_name"> <br/> <activity Android: Name = "alarmmanageractivity" <br/> Android: label = "@ string/app_name"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <explorer Android: name = "myalarmbroadcast"/> <br/> </Application> <br/> </manifest>

------------------------------------------------------------------

Some of the usage mechanisms of alarmmanager are called Global timers and some are called alarm clocks. Through its use, I personally think it is more appropriate to call a global timer. In fact, its role is similar to that of timer. There are two similar usage: (1) execute an operation after the specified duration (2) Periodically execute an operation

Steps:

First, obtain the manager. Alarmmanager = This. getsystemservice (alarm_service );

Second, use the set and setrepeating methods of alarmmanager for timing. You can use the cancel () method to cancel the timer.

In the set and setrepeating methods, the first parameter type represents the scheduled mode. The scheduled mode mainly includes the following types.

 

 

 

 

 

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.