Alarmmanager and alarm

Source: Internet
Author: User
Package mars.com; import android. app. activity; import android. app. alarmmanager; import android. app. pendingintent; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; public class demoserviceactivity extends activity {private button button1; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); button1 = (button) findviewbyid (R. id. button1); Final alarmmanager = (alarmmanager) getsystemservice (alarm_service); Final intent = new intent (); intent. setaction ("mars.com. my_action "); intent. putextra ("message", "coming off duty"); Final pendingintent = pendingintent. getbroadcast (getapplicationcontext (), 0, intent, 0); Final long time = system. currenttimemillis (); // set the current time to button1.setonclicklistener (New onclicklistener () {public void onclick (view v) {alarmmanager. setrepeating (alarmmanager. rtc_wakeup, time, 8000, pendingintent); // you can specify an exact duplicate reminder. // alarmmanager. set (alarmmanager. RTC, time, pendingintent); // set a single alarm reminder // alarmmanager. setinexactrepeating (alarmmanager. rtc_wakeup, time, // 8000, pendingintent); // sets an inaccurate duplicate reminder // alarmmanager. cancel (pendingintent); // cancel the alarm }});}}

Package mars.com; import android. content. broadcastreceiver; import android. content. context; import android. content. intent; import android. widget. toast; public class myreceiver extends broadcastreceiver {@ overridepublic void onreceive (context, intent) {toast. maketext (context, "trigger a single alarm" + intent. getstringextra ("message"), toast. length_short ). show ();}}

Declare aggreger

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="mars.com"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk android:minSdkVersion="8" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".DemoServiceActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <receiver android:name="MyReceiver" >            <intent-filter>                <action android:name="mars.com.MY_ACTION" >                </action>            </intent-filter>        </receiver>    </application></manifest>

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.