This example lets the user set an alarm by Timepickerdialog the Time Selection dialog box. and activate the alarm activity at a specified time by alarmmanager the global timer.
Program execution:
Instance code:
Package Com.jph.alarm.test;import Java.util.calendar;import Android.app.activity;import Android.app.AlarmManager; Import Android.app.pendingintent;import Android.app.service;import Android.app.timepickerdialog;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.button;import android.widget.timepicker;import android.widget.toast;/** * Describe:</br > * </br> Simple alarm Reminder * </br> This instance via Timepickerdialog Time Selection dialog * </br> Let the user set the alarm and pass the Alarmmanager global timer * </BR > Activate alarm at specified time Activity * </br> @author jph * </br>date:2014.07.29 * */public class Alarmtest extends activity{ Button settime; Alarmmanager Amanager; Calendar currenttime = Calendar.getinstance (); @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get the program Interface Buttonsettime = (Button) Findviewbyid ( R.id.settime);//Get Alarmmanager object Amanager = (Alarmmanager) getsystEmservice (Service.alarm_service);//For the "Set Alarm" button to bind the listener. Settime.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View source) {Calendar currenttime = Calendar.getinstance ();//Create a Timepickerdialog instance and display it. New Timepickerdialog (alarmtest.this, 0,//Bind listener New Timepickerdialog.ontimesetlistener () {@Overridepublic void Ontimeset (Timepicker tp,int hourofday, int minute) {//Specify start Alarmactivity component Intent Intent = new intent (); Intent.setaction ( "Com.jph.alarm.test.ACTION");//Create Pendingintent object pendingintent pi = pendingintent.getactivity (alarmtest.this, 0, Intent, 0); Calendar C = calendar.getinstance ();//Set Calendar object C.set (Calendar.hour_of_day, Hourofday) based on user selection time; C.set ( Calendar.minute, MINUTE);//Set Alarmmanager to start the specified component Amanager.set at the corresponding time in the calendar (Alarmmanager.rtc_wakeup, C.gettimeinmillis (), pi);//Displays the alarm settings for a successful message toast.maketext (Alarmtest.this, "Alarm setup succeeded", Toast.length_short). Show ();}, Currenttime.get (Calendar.hour_of_day), Currenttime.get (Calendar.minute), false). Show ();}});}}
Package Com.jph.alarm.test;import Java.util.calendar;import Android.app.activity;import Android.app.AlarmManager; Import Android.app.pendingintent;import Android.app.service;import Android.app.timepickerdialog;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener ; Import Android.widget.button;import android.widget.timepicker;import android.widget.toast;/** * Describe:</br > * </br> Simple alarm Reminder * </br> This instance via Timepickerdialog Time Selection dialog * </br> Let the user set the alarm and pass the Alarmmanager global timer * </BR > Activate alarm at specified time Activity * </br> @author jph * </br>date:2014.07.29 * */public class Alarmtest extends activity{ Button settime; Alarmmanager Amanager; Calendar currenttime = Calendar.getinstance (); @Overridepublic void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//Get the program Interface Buttonsettime = (Button) Findviewbyid ( R.id.settime);//Get Alarmmanager object Amanager = (Alarmmanager) getsystEmservice (Service.alarm_service);//For the "Set Alarm" button to bind the listener. Settime.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View source) {Calendar currenttime = Calendar.getinstance ();//Create a Timepickerdialog instance and display it.New Timepickerdialog (alarmtest.this, 0,//Bind listener New Timepickerdialog.ontimesetlistener () {@Overridepublic void Ontimeset (Timepicker tp,int hourofday, int minute) {//Specify start Alarmactivity component Intent Intent = new intent (); Intent.setaction ( "Com.jph.alarm.test.ACTION");//Create Pendingintent object pendingintent pi = pendingintent.getactivity (alarmtest.this, 0, Intent, 0); Calendar C = calendar.getinstance ();//Set Calendar object C.set (Calendar.hour_of_day, Hourofday) based on user selection time; C.set ( Calendar.minute, MINUTE);//Set Alarmmanager to start the specified component Amanager.set at the corresponding time in the calendar (Alarmmanager.rtc_wakeup, C.gettimeinmillis (), pi);//Displays the alarm settings for a successful message toast.maketext (Alarmtest.this, "Alarm setup succeeded", Toast.length_short). Show ();}, Currenttime.get (Calendar.hour_of_day), Currenttime.get (Calendar.minute), false). Show ();}});}}
Androidmanifest.xml
<?XML version= "1.0" encoding= "Utf-8"?
><manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.jph.alarm.test" Android: Versioncode= "1" android:versionname= "1.0" ><uses-sdkandroid:minsdkversion= "7" android:targetsdkversion= "18" /><application android:icon= "@drawable/ic_launcher" android:label= "@string/app_name" ><activity Android:name= ". Alarmtest "android:label=" @string/app_name "><intent-filter><action android:name=" Android.intent.action.MAIN "/><category android:name=" Android.intent.category.LAUNCHER "/></ Intent-filter></activity><activity android:name= ". Alarmactivity "> <intent-filter> <action android:name=" Com.jph.alarm.test.ACTION "/> <category an Droid:name= "Android.intent.category.DEFAULT" ></category></intent-filter></activity></ Application></manifest>
Alarm Alerts for Android development instances