刚开始做东西得时候很不了解android中一些组件的应用方法,找了一个闹钟的小例子来更好的理解广播的接收.

   应用程序的界面是这样的,这个例子是用闹钟的制定与解除来穿插broadcast  Receiver 的知识,不说废话了,先看下应用程序的界面:

   

  点击设定闹钟的按钮,弹出来一个设置时间的对话框,你选择时间之后会弹出一个Toast告诉你定的时间时多少,然后到时间之后弹出对话框提示时间到。先看图:

是一个小闹钟的完整过程,代码中关键的部分就是用到AlarmManager控制时间和PendingIntent 捕获要执行的广播,先贴代码再详细讲解代码。

 
mButton1
.
setOnClickListener(
new
 
View
.
OnClickListener()
 
 
 
 
{
 
 
 
 
 
 
public
 
void
 
onClick(View
 
v)
 
 
 
 
 
 
{
 
/*
 
取得按下按钮时的时间做为TimePickerDialog的默认值
 
*/
 
 
 
 
 
 
 
 
c
.
setTimeInMillis(System
.
currentTimeMillis());
 
 
 
 
 
 
 
 
startCalendar
.
setTimeInMillis(System
.
currentTimeMillis());
 
 
 
 
 
 
 
 
int
 
mHour
 
=
 
c
.
get(Calendar
.
HOUR_OF_DAY);
 
 
 
 
 
 
 
 
int
 
mMinute
 
=
 
c
.
get(Calendar
.
MINUTE);
 
/*
 
跳出TimePickerDialog来设定时间
 
*/
 
 
 
 
 
 
 
 
new
 
TimePickerDialog(SetAlarmActivity
.
this
,
10
 
 
 
 
 
 
 
 
 
 
 
 
new
 
TimePickerDialog
.
OnTimeSetListener()
11
 
 
 
 
 
 
 
 
 
 
 
 
{
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
public
 
void
 
onTimeSet(TimePicker
 
view,
 
int
 
hourOfDay,
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
int
 
minute)
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{
 
/*
 
取得设定后的时间,秒跟毫秒设为0
 
*/
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c
.
setTimeInMillis(System
.
currentTimeMillis());
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c
.
set(Calendar
.
HOUR_OF_DAY,
 
hourOfDay);
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c
.
set(Calendar
.
MINUTE,
 
minute);
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c
.
set(Calendar
.
SECOND,
 
0
);
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c
.
set(Calendar
.
MILLISECOND,
 
0
);
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Intent
 
intent
 
=
 
new
 
Intent(SetAlarmActivity
.
this
,
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
CallAlarm
.
class
);
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
PendingIntent
 
sender
 
=
 
PendingIntent
.
getBroadcast(
23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
SetAlarmActivity
.
this
,
 
0
,
 
intent,
 
0
);
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
AlarmManager
 
am;
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
am
 
=
 
(AlarmManager)
 
getSystemService(ALARM_SERVICE);
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
am
.
set(AlarmManager
.
RTC_WAKEUP,
 
c
.
getTimeInMillis(),
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
sender);
 
/*
 
更新显示的设定闹钟时间
 
*/
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
String
 
tmpS
 
=
 
format(hourOfDay)
 
+
 
"
"
 
+
 
format(minute);
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
setTime1
.
setText(tmpS);
 
/*
 
以Toast提示设定已完成
 
*/
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Toast
.
makeText(SetAlarmActivity
.
this
,
 
"
设定闹钟时间为
"
 
+
 
tmpS,
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Toast
.
LENGTH_SHORT)
.
show();
32
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
}
34
 
 
 
 
 
 
 
 
 
 
 
 
}
,
 
mHour,
 
mMinute,
 
true
)
.
show();
35
 
 
 
 
 
 
}
36
 
 
 
 
}
);
 

代码中显示了当点击按钮之后所进行的代码处理,我们获得了设置的时间,然后用了一个Intent,用了一个pendIntent,这个PendIntent在捕获广播的时候才会启动,然后我们将这个PendIntent绑定到AlarmManager上面,有三个参数,一个是时间格式,一个是响铃的时间,一个是闹铃到期时启动sender这个PendIntent对象,然后启动绑定在PendIntent里面的intent,从而启动广播CallAlarm。

在广播接收器CallAlar中,并不是直接执行代码,广播接收器只有一个onReceive方法,在这个方法里面又启动了另外一个Activity。需要说明的是广播接收器是没有界面的.

package
 
com
.
cz;
 
/*
 
import相关class
 
*/
import
 
android
.
content
.
Context;
import
 
android
.
content
.
Intent;
import
 
android
.
content
.
BroadcastReceiver;
import
 
android
.
os
.
Bundle;
 
/*
 
调用闹钟Alert的Receiver
 
*/
public
 
class
 
CallAlarm
 
extends
 
BroadcastReceiver
{
10
 
 
@Override
11
 
 
public
 
void
 
onReceive(Context
 
context,
 
Intent
 
intent)
12
 
 
{
 
/*
 
create
 
Intent,调用AlarmAlert.class
 
*/
13
 
 
 
 
Intent
 
i
 
=
 
new
 
Intent(context,
 
AlarmAlert
.
class
);
14
 
 
 
 
Bundle
 
bundleRet
 
=
 
new
 
Bundle();
15
 
 
 
 
bundleRet
.
putString(
"
STR_CALLER
"
,
 
"
"
);
16
 
 
 
 
i
.
putExtras(bundleRet);
17
 
 
 
 
i
.
addFlags(Intent
.
FLAG_ACTIVITY_NEW_TASK);
18
 
 
 
 
context
.
startActivity(i);
19
 
 
}
20
}
 

启动另外一个Activity,是弹出闹铃时间到时的提示对话框,看下代码:

package
 
com
.
cz;
 
/*
 
import相关class
 
*/
import
 
android
.
app
.
Activity;
import
 
android
.
app
.
AlertDialog;
import
 
android
.
content
.
DialogInterface;
import
 
android
.
os
.
Bundle;
public
 
class
 
AlarmAlert
 
extends
 
Activity
{
10
 
 
@Override
11
 
 
protected
 
void
 
onCreate(Bundle
 
savedInstanceState)
12
 
 
{
13
 
 
 
 
super
.
onCreate(savedInstanceState);
 
/*
 
跳出的闹铃警示
 
*/
14
 
 
 
 
new
 
AlertDialog
.
Builder(AlarmAlert
.
this
)
.
setIcon(R
.
drawable
.
clock)
15
 
 
 
 
 
 
 
 
.
setTitle(
"
闹钟响了!!
"
)
.
setMessage(
"
赶快起床吧!!!
"
)
.
setPositiveButton(
16
 
 
 
 
 
 
 
 
 
 
 
 
"
关掉他
"
,
 
new
 
DialogInterface
.
OnClickListener()
17
 
 
 
 
 
 
 
 
 
 
 
 
{
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
public
 
void
 
onClick(DialogInterface
 
dialog,
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
int
 
whichButton)
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{
 
/*
 
关闭Activity
 
*/
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
AlarmAlert
.
this
.
finish();
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
}
23
 
 
 
 
 
 
 
 
 
 
 
 
}
)
.
show();
24
 
 
}
25
}
 

如果条件允许,还可以在响铃时加上震动,铃声等选项..

附上源码.