The example of this article describes the Setup method of the Android programming alarm clock. Share to everyone for your reference, specific as follows:
Alarm clock is the most common in life, in Android can be alarmmanager to implement the alarm clock, Alarmmanager class is designed to set at a specified time to complete the specified time. Alarmmanager will execute these events through the OnReceive () method, even if the system is on standby and will not affect the operation. The service can be obtained through the Context.getsystemservice method. There are a number of methods in Alarmmanager, as follows:
method |
description |
Cancel |
Cancel alarmmanager service |
tr>
set |
setting alar Mmanager service |
setinexactrepeating "/ strong> |
Set imprecise cycle |
setrepeating |
Set recurrence |
settimezone |
set time zone | " /tr>
To implement the alarm, you first need to create a class that inherits from Broadcastreceiver, implement the OnReceive method to accept the alarm service, and then invoke the Pendingintent component by establishing a intent and alarm connection. Set the alarm time by Timerpickerdialog, and when the time is up to the time we specify, the Onreceiver method receives the interface after the alarm service.
First, the Alarmreceiver class that accepts the alarm service is implemented, prompting the user with the Toast class
public class Alarmreceiver extends Broadcastreceiver {
@Override public
void OnReceive (context arg0, Intent arg1 {
//TODO auto-generated method Stub
toast.maketext (Arg0, "You set the alarm time to", Toast.length_long). Show ();
Because of the use of the Broadcastreceiver service, it is necessary to declare it again in Androidmanifest.xml:
<receiver
android:name= ". Alarmreceiver "
android:process=": Remote ">
</receiver>
Then you need to set the alarm and cancel the alarm time for listening:
Package cn.edu.pku;
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.widget.Button;
Import Android.widget.TextView;
Import Android.widget.TimePicker;
The public class Alarmactivity extends activity {/** called the ' when ' is the ' The activity ' is a-a-created.
Button MButton2;
TextView Mtextview;
Calendar Calendar;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);
Calendar=calendar.getinstance ();
mtextview= (TextView) Findviewbyid (R.ID.TEXTVIEW01);
mbutton1= (Button) Findviewbyid (R.ID.BUTTON01);
Mbutton2= (Button) Findviewbyid (R.ID.BUTTON02); Mbutton1.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//TODO auto-generate D MeThod stub Calendar.settimeinmillis (System.currenttimemillis ());
int hour = Calendar.get (Calendar.hour_of_day);
int minute = Calendar.get (Calendar.minute); New Timepickerdialog (Alarmactivity.this, New Timepickerdialog.ontimesetlistener () {public void Ontimeset (TimePi Cker view, int hourofday, int minute) {//TODO auto-generated method stub Calendar.settimeinmilli
S (System.currenttimemillis ());
Calendar.set (Calendar.hour_of_day, hourofday);
Calendar.set (Calendar.minute, MINUTE);
Calendar.set (Calendar.second, 0);
Calendar.set (Calendar.millisecond, 0);
Intent Intent = new Intent (alarmactivity.this, Alarmreceiver.class);
Pendingintent pendingintent = pendingintent.getbroadcast (alarmactivity.this, 0, intent, 0);
Alarmmanager Alarmmanager = (alarmmanager) getsystemservice (Alarm_service); Alarmmanager.set (Alarmmanager.rtc_wakeup, CalenDar.gettimeinmillis (), pendingintent); Alarmmanager.setrepeating (Alarmmanager.rtc_wakeup, System.currenttimemillis () + (10 * 1000), (24 * 60 * 60
* 1000), pendingintent);
String tmps = "Set alarm time for" + format (hourofday) + ":" +format (minute);
Mtextview.settext (Tmps);
}, hour, minute, true). Show ();
}
}); Mbutton2.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//TODO auto-generate
D method Stub Intent Intent = new Intent (alarmactivity.this, Alarmreceiver.class);
Pendingintent pendingintent = pendingintent.getbroadcast (alarmactivity.this, 0, intent, 0);
Alarmmanager Alarmmanager = (alarmmanager) getsystemservice (Alarm_service);
Alarmmanager.cancel (pendingintent); Mtextview.settext (the alarm has been canceled!)
");
}
});
private string format (int time) {string str = ' + time;
if (str.length () = = 1) {str = "0" + str;return str;
}
}
The effect is as follows:
Set Alarm
Now time to set the alarm time:
Cancel Alarm:
For more information on Android-related content readers can view the site: "Android date and time Operation tips Summary", "Android Development introduction and Advanced Course", "Android debugging techniques and common problems solution summary", " Android Multimedia How-to Summary (audio, video, audio, etc), summary of Android Basic components usage, Android View tips Summary, Android layout layout tips and a summary of Android controls usage
I hope this article will help you with the Android program.