Now the general mobile phone will have a function of the alarm clock, if using Android to implement an alarm clock can be implemented using Atarmmanager. Atarmmanager provides a system-level prompt service that allows you to schedule a service to be executed at some time in the future. Alarmmanager objects are generally not instantiated directly, but are obtained by means of the Context.getsystemservice (Context.alarm_service) method.
Here is the use of the previous timepickerdialog combined with Alarmmanager, to achieve a can set any time and can repeat the alarm, the demo effect is as follows (probably every 23:48 alarm clock notification):
Layout file code in Activity_main.xml:
1<?xml version= "1.0" encoding= "Utf-8"?>2<LinearLayout3Xmlns:android= "Http://schemas.android.com/apk/res/android"4Xmlns:tools= "Http://schemas.android.com/tools"5Android:id= "@+id/activity_main"6Android:layout_width= "Match_parent"7android:layout_height= "Match_parent"8android:orientation= "Vertical"9tools:context= "Com.example.administrator.alarmdemo.MainActivity" >Ten<TextView OneAndroid:id= "@+id/time_tv" AAndroid:layout_width= "Match_parent" -android:layout_height= "Wrap_content" -Android:hint= "Please set the alarm time" theAndroid:clickable= "true" -android:onclick= "SetTime" -android:gravity= "Center"/> -<TextView +Android:id= "@+id/time_tv2" -Android:layout_width= "Match_parent" +android:layout_height= "Wrap_content" AAndroid:hint= "Please set the ring interval time" atAndroid:clickable= "true" -android:onclick= "Setintervaltime" -android:gravity= "Center"/> -<Button -Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" inandroid:onclick= "Open" -android:text= "Turn on Alarm"/> to<Button +Android:layout_width= "Match_parent" -android:layout_height= "Wrap_content" theandroid:onclick= "Stop" *android:text= "End Alarm"/> $</LinearLayout>
Androidmainfest.xml configuration file (add custom repeat alarm receiver):
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.administrator.alarmdemo" >4<Application5Android:allowbackup= "true"6android:icon= "@mipmap/ic_launcher"7Android:label= "@string/app_name"8Android:supportsrtl= "true"9Android:theme= "@style/apptheme" >Ten<activity android:name= ". Mainactivity "> One<intent-filter> A<action android:name= "Android.intent.action.MAIN"/> - -<category android:name= "Android.intent.category.LAUNCHER"/> the</intent-filter> -</activity> -<receiver -Android:name= ". Repeatalarmreceiver " +Android:enabled= "true" -Android:exported= "true" > +<intent-filter> A<action android:name= "Repeatalarm"/> at</intent-filter> -</receiver> -<activity android:name= ". Main2activity "></activity> -</application> - -</manifest>
Repeatalarmreceiver.java broadcast receive file:
1 ImportAndroid.content.BroadcastReceiver;2 ImportAndroid.content.Context;3 Importandroid.content.Intent;4 ImportAndroid.widget.Toast;5 6 Public classRepeatalarmreceiverextendsBroadcastreceiver {7 PublicRepeatalarmreceiver () {8 }9 @OverrideTen Public voidOnReceive (Context context, Intent Intent) { OneToast.maketext (Context, "repeating alarm", Toast.length_short). Show (); A } -}
Mainactivity.java Code implementation:
1 ImportAndroid.app.AlarmManager;2 Importandroid.app.PendingIntent;3 ImportAndroid.app.TimePickerDialog;4 Importandroid.content.Intent;5 Importandroid.provider.Settings;6 Importandroid.support.v7.app.AppCompatActivity;7 ImportAndroid.os.Bundle;8 ImportAndroid.view.View;9 ImportAndroid.widget.TextView;Ten ImportAndroid.widget.TimePicker; One A ImportJava.util.Calendar; - - Public classMainactivityextendsappcompatactivity { the Alarmmanager Alarmmanagerset; - pendingintent Pendingintentset; - intMyhour,myminute;//set the ring time - intIntervalhour,intervalminute,intervalsecond;//set interval time (Ring cycle) +Calendar Calendar;//declaring a calendar - TextView time_tv,time_tv2; + A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); -alarmmanagerset=(Alarmmanager) Getsystemservice (alarm_service); -Calendar =calendar.getinstance (); -Myhour=Calendar.get (calendar.hour_of_day); inMyminute=Calendar.get (calendar.minute); - toIntervalhour=Calendar.get (calendar.hour_of_day); +Intervalminute=Calendar.get (calendar.minute); - thetime_tv=(TextView) Findviewbyid (R.ID.TIME_TV); *Time_tv2=(TextView) Findviewbyid (R.ID.TIME_TV2); $ }Panax Notoginseng Public voidsettime (view view) { -Timepickerdialog Timepickerdialog =NewTimepickerdialog (mainactivity. This,NewTimepickerdialog.ontimesetlistener () { the @Override + Public voidOntimeset (Timepicker view,intHourofday,intminute) { AMyhour=Hourofday; theMyminute=minute; +Time_tv.settext (myhour+ ":" +myminute); - } $},myhour,myminute,true); $ timepickerdialog.show (); - } - Public voidsetintervaltime (view view) { theTimepickerdialog Timepickerdialog =NewTimepickerdialog (mainactivity. This,NewTimepickerdialog.ontimesetlistener () { - @OverrideWuyi Public voidOntimeset (Timepicker view,intHourofday,intminute) { theIntervalhour=Hourofday; -Intervalminute=minute; WuTime_tv2.settext (intervalhour+ ":" +intervalminute); - } About},intervalhour,intervalminute,true); $ timepickerdialog.show (); - } - Public voidOpen (view view) { -String[] Triggertime=time_tv.gettext (). toString (). Split (":"); AString[] Intervaltime=time_tv2.gettext (). toString (). Split (":"); + LongTriggeratmillis= Integer.parseint (triggertime[0]) *1000*60*60+integer.parseint (triggertime[1]) *60*1000+System.currenttimemillis (); the LongIntervalmillis=integer.parseint (Intervaltime[0]) *1000*60*60+integer.parseint (intervaltime[1]) *60*1000; -Pendingintentset = Pendingintent.getbroadcast ( This, 0,NewIntent ("Repeatalarm"), 0); $ alarmmanagerset.setrepeating (alarmmanager.rtc_wakeup,triggeratmillis,intervalmillis,pendingintentset); the } the Public voidStop (View v) { the Alarmmanagerset.cancel (pendingintentset); the } -}
Example of Alarmmanager use in Android (continuous update)