Android--Use of alarm clock service (start and stop)

Source: Internet
Author: User

1.

2. Implementing the Code

 Public classFirstactivityextendsActivity {Private Static FinalString am_action = "Iflab.test.AM_ACTION";//Action Constant    /**Called when the activity is first created.*/@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.main); Button BTN1= (Button) Findviewbyid (R.id.button1);//Get ObjectButton btn2 =(Button) Findviewbyid (R.id.button2); FinalAlarmmanager Alarmmanager = (alarmmanager) getsystemservice (Alarm_service);//Get System ServicesIntent Intent =NewIntent ();//Intent ObjectIntent.setaction (am_action);//Set ActionIntent.putextra ("message", "It's time to get off the clock!" ");//Passing Strings        FinalPendingintent pendingintent =pendingintent.getbroadcast (firstactivity. This, 0, intent, 0); Final LongTime = System.currenttimemillis ();//set the current timeBtn1.setonclicklistener (NewView.onclicklistener () {//Listener@Override Public voidOnClick (View v) {//TODO auto-generated Method Stub//Set recurring alarm remindersalarmmanager.setrepeating (Alarmmanager.rtc_wakeup, Time,8 * 1000, pendingintent);        }        }); Btn2.setonclicklistener (NewView.onclicklistener () {//Listener@Override Public voidOnClick (View v) {//TODO auto-generated Method StubAlarmmanager.cancel (pendingintent);//Cancel Alarm            }        }); }}
firstactivity
 Public class extends broadcastreceiver{    @Override    publicvoid  onreceive (Context arg0, Intent arg1 ) {        //  TODO auto-generated method stub        String str;        str= "Trigger duplicate alarm:" +arg1.getstringextra ("message");        Toast.maketext (arg0, Str,toast.length_long). Show ();         // Show     }}
Myreceiver
<receiver android:name= "Myreceiver" >            <intent-filter>                <action android:name= "iflab.test.AM_ ACTION "/>            </intent-filter>        </receiver>

Android--Use of alarm clock service (start and stop)

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.