Alarm notification for Android development instances

Source: Internet
Author: User

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

 
 
  
  
   
      
          
   
  
  



Related Article

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.