Android编程闹钟设置方法详解
本文实例讲述了android编程闹钟设置方法。分享给大家供大家参考,具体如下:
闹钟在生活中最常见了,在android中可以通过alarmmanager来实现闹钟,alarmmanager类专门用来设置在某个指定的时间去完成指定的时间。alarmmanager就会通过onreceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行。可以通过context.getsystemservice方法来获得该服务。alarmmanager中的方法不少,如下:
方法 |
说明 |
cancel |
取消alarmmanager服务 |
set |
设置alarmmanager服务 |
setinexactrepeating |
设置不精确周期 |
setrepeating |
设置重复周期 |
settimezone |
设置时区 |
要实现闹钟,首先需要创建一个继承自broadcastreceiver的类,实现onreceive方法来接受这个alarm服务,然后通过建立intent和pendingintent连接来调用alarm组件。通过timerpickerdialog来设置闹铃时间,当时间到了我们指定的时间后onreceiver方法接受到alarm服务后的界面。
首先实现接受alarm服务的alarmreceiver类,用toast类提示用户
public class alarmreceiver extends broadcastreceiver { @override public void onreceive(context arg0, intent arg1) { // todo auto-generated method stub toast.maketext(arg0, "你设置的闹铃时间到了", toast.length_long).show(); } }
由于使用了broadcastreceiver服务,因此需要再androidmanifest.xml中进行声明:
<receiver android:name=".alarmreceiver" android:process=":remote"> </receiver>
然后需要设置闹铃和取消闹铃的时间进行监听:
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; public class alarmactivity extends activity { /** called when the activity is first created. */ button mbutton1; 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-generated 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(timepicker view, int hourofday, int minute) { // todo auto-generated method stub calendar.settimeinmillis(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 = "设置闹钟时间为" + format(hourofday) + ":" +format(minute); mtextview.settext(tmps); } }, hour, minute, true).show(); } }); mbutton2.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { // todo auto-generated 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("闹铃已取消!"); } }); } private string format(int time){ string str = "" + time; if(str.length() == 1){ str = "0" + str; } return str; } }
效果如下:
设置闹铃
现在时间到设置闹铃的时间:
取消闹铃:
更多关于android相关内容感兴趣的读者可查看本站专题:《android日期与时间操作技巧总结》、《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android多媒体操作技巧汇总(音频,视频,录音等)》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
上一篇: 详解SpringBoot注入数据的方式