本文实例讲述了android手机闹钟用法。分享给大家供大家参考。具体如下:
一、开发手机闹钟主要用到了alarmmanager类,alarmmanager类提供了访问系统定时服务的途径,开发人员可以在程序中设置某个应用程序在未来的某个时刻被执行。当alarmmanager定时时间到了之后,当初注册的intent对象将会被系统广播,进而启动目标程序。
二、在程序运行时当需要使用alarmmanager类时,可以通过context对象的getsystemservice(context.alarm_service)方法来获得alarmmanager对象。
以下程序中使用时间选择对话框设置闹钟的时间。
main.xml布局文件:
1
2
3
4
5
6
7
8
9
10
|
<?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" > <button android:id= "@+id/btn" android:text= "设置闹钟" android:layout_width= "fill_parent" android:layout_height= "wrap_content" /> </linearlayout> |
清单文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns:android= "http://schemas.android.com/apk/res/android" package = "com.ljq.activity" android:versioncode= "1" android:versionname= "1.0" > <application android:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ".mainactivity" android:label= "@string/app_name" > <intent-filter> <action android:name= "android.intent.action.main" /> <category android:name= "android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name= ".alarmactivity" /> <!-- android:process= ":remote" : 新开一个进程 --> <receiver android:name= ".alarmreceiver" android:process= ":remote" /> </application> <uses-sdk android:minsdkversion= "7" /> </manifest> |
alarmreceiver类:
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.ljq.activity; import android.content.broadcastreceiver; import android.content.context; import android.content.intent; public class alarmreceiver extends broadcastreceiver{ @override public void onreceive(context context, intent intent) { intent i= new intent(context, alarmactivity. class ); i.addflags(intent.flag_activity_new_task); context.startactivity(i); } } |
alarmactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.ljq.activity; import android.app.activity; import android.app.alertdialog; import android.content.dialoginterface; import android.content.dialoginterface.onclicklistener; import android.os.bundle; public class alarmactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); //显示对话框 new alertdialog.builder(alarmactivity. this ). settitle( "闹钟" ). //设置标题 setmessage( "时间到了!" ). //设置内容 setpositivebutton( "知道了" , new onclicklistener(){ //设置按钮 public void onclick(dialoginterface dialog, int which) { alarmactivity. this .finish(); //关闭activity } }).create().show(); } } |
mainactivity类:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
package com.ljq.activity; import java.util.calendar; import android.app.activity; import android.app.alarmmanager; import android.app.dialog; import android.app.pendingintent; import android.app.timepickerdialog; import android.content.context; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.button; import android.widget.timepicker; import android.widget.toast; public class mainactivity extends activity{ private button btn= null ; private alarmmanager alarmmanager= null ; calendar cal=calendar.getinstance(); final int dialog_time = 0 ; //设置对话框id @override protected void oncreate(bundle savedinstancestate) { super .oncreate(savedinstancestate); setcontentview(r.layout.main); alarmmanager=(alarmmanager)getsystemservice(context.alarm_service); btn=(button)findviewbyid(r.id.btn); btn.setonclicklistener( new view.onclicklistener(){ public void onclick(view view) { showdialog(dialog_time); //显示时间选择对话框 } }); } @override protected dialog oncreatedialog( int id) { dialog dialog= null ; switch (id) { case dialog_time: dialog= new timepickerdialog( this , new timepickerdialog.ontimesetlistener(){ public void ontimeset(timepicker timepicker, int hourofday, int minute) { calendar c=calendar.getinstance(); //获取日期对象 c.settimeinmillis(system.currenttimemillis()); //设置calendar对象 c.set(calendar.hour, hourofday); //设置闹钟小时数 c.set(calendar.minute, minute); //设置闹钟的分钟数 c.set(calendar.second, 0 ); //设置闹钟的秒数 c.set(calendar.millisecond, 0 ); //设置闹钟的毫秒数 intent intent = new intent(mainactivity. this , alarmreceiver. class ); //创建intent对象 pendingintent pi = pendingintent.getbroadcast(mainactivity. this , 0 , intent, 0 ); //创建pendingintent //alarmmanager.set(alarmmanager.rtc_wakeup, c.gettimeinmillis(), pi); //设置闹钟 alarmmanager.set(alarmmanager.rtc_wakeup, system.currenttimemillis(), pi); //设置闹钟,当前时间就唤醒 toast.maketext(mainactivity. this , "闹钟设置成功" , toast.length_long).show(); //提示用户 } }, cal.get(calendar.hour_of_day), cal.get(calendar.minute), false ); break ; } return dialog; } } |
运行结果:
希望本文所述对大家的android程序设计有所帮助。