写代码挺费时间的,懒得写,我就说说思路,你建一个类继承子BroadcastReceiver,复写onReceive方法,在里面写监听系统时间的代码用于跟你闹钟设置时间进行匹配,当匹配上时,创建MediaPlayer对象,播放你要播放的音乐,音乐文件可以放在Res/raw目录下,也可是在SD卡中,使用不同的方法即可。设闹钟的Activity找找例子,写出来应该不难。
在Android中可以通过AlarmManager 来实现闹钟,AlarmManager类是专门用来设定在某个指定的时间去完成指定的事件。AlarmManager 提供了访问系统警报的服务,只要在程序中设置了警报服务,AlarmManager 就会通过onReceive()方法去执行这些事件,就算系统处于待机状态,同样不会影响运行。可以通过 Context.getSystemService 方法来获得该服务。 AlarmManager 中的方法很少。如下所示
AlarmManager 的方法
方法 说明
cancel 取消AlarmManager服务
set 设置AlarmManager服务
setInexactRepeating 设置不精确周期
setRepeating 设置精确周期
setTimeZone 设置时区
要实现闹钟,首先需要创建一个继承自 BroadcastReceiver 的类,实现 onReceive 方法来接收这个Alarm服务,然后通过建立Intent 和 PendingIntent连接来调用 Alarm组件。当我们点击“设置闹钟”按钮时,通过 TimePickerDialog 来设置时间,当时间到我们指定的时间后onReceive 方法接收Alarm服务 运行效果见下图
设置闹钟时间
时间到后的Toast提示
首先看看我们实现的接收 Alarm 服务的 AlarmReceiver类,很简单,就是在收到消息后用一个 Toast 来提示用户,代码如下:
Java代码
1. packagecom.yarin.android.Examples_07_07;
2.
3. importandroid.content.BroadcastReceiver;
4. importandroid.content.Context;
5. importandroid.content.Intent;
6. importandroid.widget.Toast;
7.
8. publicclassAlarmReceiverextendsBroadcastReceiver{
9. publicvoidonReceive(Contextcontext,Intentintent){
10. "你设置的闹钟时间到了",Toast.LENGTH_LONG).show();
11. }
12. }
package com.yarin.android.Examples_07_07; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Toast.makeText(context, "你设置的闹钟时间到了", Toast.LENGTH_LONG).show(); } }
由于使用了 BroadcastReceiver 服务,因此需要在 AndroidManifest.xml中进行声明,代码如下:
Xml代码
1. <receiverandroid:name=".AlarmReceiver"android:process=":remote"/>
<receiver android:name=".AlarmReceiver" android:process=":remote" />
然后就只需要对"设置闹钟"、"取消闹钟" 的事件进行监听了,代码如下:
Java代码
1. packagecom.yarin.android.Examples_07_07;
2.
3. importjava.util.Calendar;
4.
5. importandroid.app.Activity;
6. importandroid.app.AlarmManager;
7. importandroid.app.PendingIntent;
8. importandroid.app.TimePickerDialog;
9. importandroid.content.Intent;
10. importandroid.os.Bundle;
11. importandroid.view.View;
12. importandroid.widget.Button;
13. importandroid.widget.TextView;
14. importandroid.widget.TimePicker;
15.
16. publicclassActivity01extendsActivity{
17. ButtonmButton1;
18. ButtonmButton2;
19.
20. TextViewmTextView;
21.
22. Calendarcalendar;
23.
24. /**Calledwhentheactivityisfirstcreated.*/
25. @Override
26. publicvoidonCreate(BundlesavedInstanceState){
27. super.onCreate(savedInstanceState);
28.
29. setContentView(R.layout.main);
30. calendar=Calendar.getInstance();
31.
32. mTextView=(TextView)findViewById(R.id.TextView01);
33. mButton1=(Button)findViewById(R.id.Button01);
34. mButton2=(Button)findViewById(R.id.Button02);
35.
36. newView.OnClickListener(){
37. publicvoidonClick(Viewv){
38. calendar.setTimeInMillis(System.currentTimeMillis());
39. intmHour=calendar.get(Calendar.HOUR_OF_DAY);
40. intmMinute=calendar.get(Calendar.MINUTE);
41. newTimePickerDialog(Activity01.this,
42. newTimePickerDialog.OnTimeSetListener(){
43. publicvoidonTimeSet(TimePickerview,
44. inthourOfDay,intminute){
45. calendar.setTimeInMillis(System
46. .currentTimeMillis());
47. calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
48. calendar.set(Calendar.MINUTE,minute);
49. 0);
50. 0);
51. /*建立Intent和PendingIntent,来调用目标组件*/
52. newIntent(Activity01.this,
53. class);
54. PendingIntentpendingIntent=PendingIntent
55. this,0,
56. 0);
57. AlarmManageram;
58. /*获取闹钟管理的实例*/
59. am=(AlarmManager)getSystemService(ALARM_SERVICE);
60. /*设置闹钟*/
61. am.set(AlarmManager.RTC_WAKEUP,calendar
62. .getTimeInMillis(),pendingIntent);
63. /*设置周期闹*/
64. am.setRepeating(AlarmManager.RTC_WAKEUP,System
65. .currentTimeMillis()
66. 10*1000),(24*60*60*1000),
67. pendingIntent);
68. "设置闹钟时间为"+format(hourOfDay)
69. ":"+format(minute);
70. mTextView.setText(tmpS);
71. }
72. true).show();
73. }
74. });
75.
76. newView.OnClickListener(){
77. publicvoidonClick(Viewv){
78. newIntent(Activity01.this,AlarmReceiver.class);
79. PendingIntentpendingIntent=PendingIntent.getBroadcast(
80. this,0,intent,0);
81. AlarmManageram;
82. /*获取闹钟管理的实例*/
83. am=(AlarmManager)getSystemService(ALARM_SERVICE);
84. /*取消*/
85. am.cancel(pendingIntent);
86. "闹钟已取消!");
87. }
88. });
89. }
90.
91. /*格式化字符串(7:3->07:03)*/
92. privateStringformat(intx){
93. ""+x;
94. if(s.length()==1)
95. "0"+s;
96. returns;
97. }
98. }
package com.yarin.android.Examples_07_07; 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 Activity01 extends Activity { Button mButton1; Button mButton2; TextView mTextView; Calendar calendar; /** Called when the activity is first created. */ @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) { calendar.setTimeInMillis(System.currentTimeMillis()); int mHour = calendar.get(Calendar.HOUR_OF_DAY); int mMinute = calendar.get(Calendar.MINUTE); new TimePickerDialog(Activity01.this, new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 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和PendingIntent,来调用目标组件 */ Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent .getBroadcast(Activity01.this, 0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am = (AlarmManager) getSystemService(ALARM_SERVICE); /* 设置闹钟 */ am.set(AlarmManager.RTC_WAKEUP, calendar .getTimeInMillis(), pendingIntent); /* 设置周期闹 */ am.setRepeating(AlarmManager.RTC_WAKEUP, System .currentTimeMillis() + (10 * 1000), (24 * 60 * 60 * 1000), pendingIntent); String tmpS = "设置闹钟时间为" + format(hourOfDay) + ":" + format(minute); mTextView.setText(tmpS); } }, mHour, mMinute, true).show(); } }); mButton2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Intent intent = new Intent(Activity01.this, AlarmReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast( Activity01.this, 0, intent, 0); AlarmManager am; /* 获取闹钟管理的实例 */ am = (AlarmManager) getSystemService(ALARM_SERVICE); /* 取消 */ am.cancel(pendingIntent); mTextView.setText("闹钟已取消!"); } }); } /* 格式化字符串(7:3->07:03) */ private String format(int x) { String s = "" + x; if (s.length() == 1) s = "0" + s; return s; } }