Alarm notification for Android development instances
In this example, you can set an alarm in the TimePickerDialog Time Selection dialog box and use the global timer of AlarmManager to start the alarm Activity at the specified time.
Program running:
Instance code:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHByZSBjbGFzcz0 = "brush: java;"> 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:
*
Simple alarm *
This instance uses the TimePickerDialog Time Selection dialog box *
Allows you to set an alarm and use the AlarmManager global timer *
Start the alarm Activity at the specified time *
@ Author jph *
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 setTime = (Button) findViewById (R. id. setTime); // get the AlarmManager object aManager = (AlarmManager) getSystemService (Service. ALARM_SE RVICE); // bind the listener to the "set alarm" button. 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 the listener new TimePickerDialog. onTimeSetListener () {@ Overridepublic void onTimeSet (TimePicker tp, int hourOfDay, int minute) {// specify to start the AlarmActivity component Intent intent = new Intent (); intent. setAction ("com. jph. alarm. test. ACTION "); // create the PendingIntent object PendingIntent pi = PendingIntent. getActivity (AlarmTest. this, 0, intent, 0); Calendar c = Calendar. getInstance (); // set the Calendar Object c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); // set AlarmManager to start the specified component aManager at the specified time in the Calendar. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // display the message Toast when the alarm is set successfully. makeText (AlarmTest. this, "the alarm is set successfully", 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:
*
Simple alarm *
This instance uses the TimePickerDialog Time Selection dialog box *
Allows you to set an alarm and use the AlarmManager global timer *
Start the alarm Activity at the specified time *
@ Author jph *
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 setTime = (Button) findViewById (R. id. setTime); // get the AlarmManager object aManager = (AlarmManager) getSystemService (Service. ALARM_SE RVICE); // bind the listener to the "set alarm" button. 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 the listener new TimePickerDialog. onTimeSetListener () {@ Overridepublic void onTimeSet (TimePicker tp, int hourOfDay, int minute) {// specify to start the AlarmActivity component Intent intent = new Intent (); intent. setAction ("com. jph. alarm. test. ACTION "); // create the PendingIntent object PendingIntent pi = PendingIntent. getActivity (AlarmTest. this, 0, intent, 0); Calendar c = Calendar. getInstance (); // set the Calendar Object c. set (Calendar. HOUR_OF_DAY, hourOfDay); c. set (Calendar. MINUTE, minute); // set AlarmManager to start the specified component aManager at the specified time in the Calendar. set (AlarmManager. RTC_WAKEUP, c. getTimeInMillis (), pi); // display the message Toast when the alarm is set successfully. makeText (AlarmTest. this, "the alarm is set successfully", Toast. LENGTH_SHORT ). show () ;}, currentTime. get (Calendar. HOUR_OF_DAY), currentTime. get (Calendar. MINUTE), false ). show ();}});}}
AndroidManifest. xml