Android alarm settings

Source: Internet
Author: User
Package com. king. android. media;

Import java. util. Calendar;

Import android. app. Activity;
Import android. app. AlarmManager;
Import android. app. PendingIntent;
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. TextView;
Import android. widget. TimePicker;

Import com. king. android. R;

/**

* Description: alarm event.
* Author: Andy. Liu
* Time: 08:03:24
**/
Public class AlarmActivity extends Activity implements OnClickListener {

Calendar mCalendar;
TextView mTextView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){

Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MCalendar = Calendar. getInstance ();

MTextView = (TextView) findViewById (R. id. TV _info );
MTextView. setVisibility (View. VISIBLE );

Button btnSetAlarm = (Button) findViewById (R. id. btn_first );
BtnSetAlarm. setText ("set alarm ");
BtnSetAlarm. setVisibility (View. VISIBLE );
BtnSetAlarm. setOnClickListener (this );

BtnSetAlarm = (Button) findViewById (R. id. btn_second );
BtnSetAlarm. setText ("Cancel alarm ");
BtnSetAlarm. setVisibility (View. VISIBLE );
BtnSetAlarm. setOnClickListener (this );
}

@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. btn_first:
SetAlarm ();
Break;
Case R. id. btn_second:
SetCancel ();
Break;
}
}

Private void setAlarm (){
MCalendar. setTimeInMillis (System. currentTimeMillis ());
Int hour = mCalendar. get (Calendar. HOUR_OF_DAY );
Int miu = mCalendar. get (Calendar. MINUTE );
New TimePickerDialog (AlarmActivity. this, new TimePickerDialog. OnTimeSetListener (){
@ Override
Public void onTimeSet (TimePicker view, int hourOfDay, int minute ){
MCalendar. setTimeInMillis (System. currentTimeMillis ());
MCalendar. set (Calendar. MINUTE, minute );
MCalendar. set (Calendar. SECOND, 0 );
MCalendar. set (Calendar. MILLISECOND, 0 );

Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
// Set the alarm
Am. set (AlarmManager. RTC_WAKEUP, mCalendar. getTimeInMillis (), pendingIntent );
// Set the periodic alarm.
Am. setRepeating (AlarmManager. RTC_WAKEUP, System. currentTimeMillis () + 10*10000, 24*60*60*1000, pendingIntent );

String tmps = "set the alarm to" + format (hourOfDay) + ":" + format (minute );
MTextView. setText (tmps );
}
}, Hour, miu, true). show ();
}


/**
*
* TODO: cancel the alarm.
* Author: Andy. Liu
* Create Time: 08:18:09
* TAG:
* Return: void
*/
Private void setCancel (){
Intent intent = new Intent (AlarmActivity. this, AlarmReceiver. class );
PendingIntent pendingIntent = PendingIntent. getBroadcast (AlarmActivity. this, 0, intent, 0 );
AlarmManager am = (AlarmManager) getSystemService (ALARM_SERVICE );
// Cancel the alarm
Am. cancel (pendingIntent );
MTextView. setText ("the alarm has been canceled ");
}

/**
*
* TODO: format the string
* Author: Andy. Liu
* Create Time: 08:16:01
* TAG: @ param x
* Return: void
*/
Private String format (int x ){
String s = x + "";
If (s. length () = 1 ){
S = "0" + s;
}
Return s;
}

}

 

Package com. king. android. media;

Import android. content. BroadcastReceiver;
Import android. content. Context;
Import android. content. Intent;
Import android. widget. Toast;

/**

* Description: Alert clock broadcast.
* Author: Andy. Liu
* Time: 08:00:38
**/
Public class AlarmReceiver extends BroadcastReceiver {

@ Override
Public void onReceive (Context context, Intent intent ){
Toast. makeText (context, "the time you set is reached", Toast. LENGTH_LONG). show ();
}

}

<Receiver android: name = ". media. AlarmReceiver" android: process = ": remote"/> Register

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.