Android编程使用AlarmManager设置闹钟的方法
程序员文章站
2023-12-16 20:58:22
本文实例讲述了android编程使用alarmmanager设置闹钟的方法。分享给大家供大家参考,具体如下:
package com.aina.android;...
本文实例讲述了android编程使用alarmmanager设置闹钟的方法。分享给大家供大家参考,具体如下:
package com.aina.android; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; import android.widget.toast; /** * com.aina.android * pro_alarmmanager * @author aina.huang e-mail: 674023920@qq.com * @version 创建时间:2010 jul 8, 2010 3:03:19 pm * 类说明 */ public class alamrreceiver extends broadcastreceiver { @override public void onreceive(context context, intent intent) { // todo auto-generated method stub toast.maketext(context, "闹钟时间到", toast.length_long).show(); } }
package com.aina.android; 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 test extends activity { /** called when the activity is first created. */ private textview tv = null; private button btn_set = null; private button btn_cel = null; private calendar c = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); tv = (textview) this.findviewbyid(r.id.textview); btn_set = (button) this.findviewbyid(r.id.button01); btn_cel = (button) this.findviewbyid(r.id.button02); c = calendar.getinstance(); btn_set.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub c.settimeinmillis(system.currenttimemillis()); int hour = c.get(calendar.hour_of_day); int minute = c.get(calendar.minute); new timepickerdialog(test.this,new timepickerdialog.ontimesetlistener(){ public void ontimeset(timepicker view, int hourofday, int minute) { // todo auto-generated method stub c.settimeinmillis(system.currenttimemillis()); c.set(calendar.hour_of_day, hourofday); c.set(calendar.minute, minute); c.set(calendar.second, 0); c.set(calendar.millisecond, 0); intent intent = new intent(test.this,alamrreceiver.class); pendingintent pi = pendingintent.getbroadcast(test.this, 0, intent, 0); alarmmanager am = (alarmmanager) getsystemservice(activity.alarm_service); am.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), pi);//设置闹钟 am.setrepeating(alarmmanager.rtc_wakeup, c.gettimeinmillis(), (10*1000), pi);//重复设置 tv.settext("设置的闹钟时间为:"+hourofday+":"+minute); } },hour,minute,true).show(); } }); btn_cel.setonclicklistener(new button.onclicklistener(){ public void onclick(view v) { // todo auto-generated method stub intent intent = new intent(test.this,alamrreceiver.class); pendingintent pi = pendingintent.getbroadcast(test.this, 0, intent, 0); alarmmanager am = (alarmmanager) getsystemservice(activity.alarm_service); am.cancel(pi); tv.settext("闹钟取消"); } }); } }
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:layout_width="fill_parent" android:id="@+id/textview" android:layout_height="wrap_content" android:text="@string/hello" /> <button android:text="设置闹钟" android:id="@+id/button01" android:layout_width="wrap_content" android:layout_height="wrap_content"> </button> <button android:text="取消闹钟" android:id="@+id/button02" android:layout_width="wrap_content" android:layout_height="wrap_content"> </button> </linearlayout>
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.aina.android" android:versioncode="1" android:versionname="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".test" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <receiver android:name=".alamrreceiver" android:process=":remote"></receiver> </application> </manifest>
ps:关于androidmanifest.xml文件相关属性功能可参考本站在线工具:
android manifest功能与权限描述大全:
http://tools.jb51.net/table/androidmanifest
更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。
推荐阅读
-
Android编程使用AlarmManager设置闹钟的方法
-
Android编程实现设置按钮背景透明与半透明及图片背景透明的方法
-
Android编程实现简单设置按钮颜色的方法
-
Android编程实现TextView字体颜色设置的方法小结
-
Android编程设置全屏的方法实例详解
-
Android编程实现获取系统内存、CPU使用率及状态栏高度的方法示例
-
Android编程使用GestureDetector实现简单手势监听与处理的方法
-
android编程实现设置、打开wifi热点共享供他人连接的方法
-
Android编程使用自定义shape实现shadow阴影效果的方法
-
Android编程实现闹钟的方法详解