Broadcast Receiver是Android四大组件之一,是Android操作系统给我们提供的一个在组件和组件之间通信的机制。Broadcast Receiver不仅可以在一个程序内部进行通信,还可以再应用程序之间进行通信。比如在某个程序消耗电量非常的多时候,可以添加这个组件进行监听,当低电量的时候可以关闭该功能。也可以监听短信的接受,当收到短信时,可以对用户进行提醒。还有就是操作系统启动完成,同样可以监听到,进而进行一系列操作。
特点:传递的信息小,频率低
Broadcast Receiver不能用来传递大数据或者高频率数据,否则会造成程序运行速度慢、等等一系列问题。
使用:
Broadcast Receiver的注册分为两种:静态注册,动态注册。
静态注册
静态注册非常简单,只需要在AndroidManifest.xml文件中添加注册信息即可。例如:在application中添加:
1
|
<receiver android:name=
"MyBC"
></receiver>
|
接着创建一个BroadcastReceiver类,用来接收广播即可,如:
1
2
3
4
5
6
7
8
|
public
class
MyBroadcast
extends
BroadcastReceiver {
// 广播接收器收到消息,执行下面的方法
@Override
public
void
onReceive(Context context, Intent intent) {
System.out.println(
"onReceive"
);
}
}
|
最后就是在需要发送广播的地方添加sendbroadcast(Intent)方法即可,例如此处我是用MainActivity发送广播,并且携带信息:
1
2
3
4
|
Intent i =
new
Intent(MainActivity.
this
,MyBroadcast.
class
);
i.putExtra(
"txt"
,
"Hello World!"
);
// 发送一个广播
sendBroadcast(i);
|
从MyBroadcast中的onReceive()方法中得到MainActivity传来的消息:
1
|
System.out.println(
"onReceive,data = "
+intent.getStringExtra(
"txt"
));
|
就可以得到Activity传来的信息了。
动态注册:
下面是Activity和Service之间的通信:
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
62
63
64
65
66
67
68
69
|
public
class
MainActivity
extends
ActionBarActivity
implements
OnClickListener {
private
Button btnActivitySendBC,btnServiceSendBC,btnRegBC,btnUnRegBC;
//实例化一个Broadcast对象
private
static
final
MyBroadCast ActivityBC =
new
MyBroadCast();
private
IntentFilter filter =
new
IntentFilter();
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获得控件
btnActivitySendBC = (Button) findViewById(R.id.btnActivitySendBC);
btnServiceSendBC = (Button) findViewById(R.id.btnServiceSendBC);
btnRegBC = (Button) findViewById(R.id.btnRegBC);
btnUnRegBC = (Button) findViewById(R.id.btnUnRegBC);
// 对控件进行监听
btnActivitySendBC.setOnClickListener(
this
);
btnServiceSendBC.setOnClickListener(
this
);
btnRegBC.setOnClickListener(
this
);
btnUnRegBC.setOnClickListener(
this
);
// 发送广播的时候,定义这一个广播的动作,可以理解为一个地址
filter.addAction(MyBroadCast.ATS);
}
@Override
public
boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return
true
;
}
@Override
public
void
onClick(View v) {
// 监听按钮
switch
(v.getId()) {
case
R.id.btnActivitySendBC:
// 发送一个广播
sendBroadcast(
new
Intent(MyBroadCast.ATS));
System.out.println(
"btnActivitySendBC"
);
break
;
case
R.id.btnServiceSendBC:
// 启动Service
startService(
new
Intent(MainActivity.
this
, MyService.
class
));
System.out.println(
"btnServiceSendBC"
);
break
;
case
R.id.btnRegBC:
// 注册一个广播
System.out.println(
"btnRegBC"
);
registerReceiver(ActivityBC, filter);
break
;
case
R.id.btnUnRegBC:
// 注销一个广播
System.out.println(
"btnUnRegBC"
);
unregisterReceiver(ActivityBC);
break
;
}
}
@Override
protected
void
onDestroy() {
//Activity停止的时候,注销Service
stopService(
new
Intent(MainActivity.
this
, MyService.
class
));
super
.onDestroy();
}
}
|
MyService:
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
|
public
class
MyService
extends
Service {
private
static
final
MyBroadCast ServiceBC =
new
MyBroadCast();
// 实例化一个广播对象
private
IntentFilter serviceFilter =
new
IntentFilter();
@Override
public
int
onStartCommand(Intent intent,
int
flags,
int
startId) {
System.out.println(
"onStartCommand"
);
serviceFilter.addAction(MyBroadCast.STA);
// 添加动作
registerReceiver(ServiceBC, serviceFilter);
// 注册Broadcast
sendBroadcast(
new
Intent(MyBroadCast.STA));
// 发送Broadcast
return
super
.onStartCommand(intent, flags, startId);
}
@Override
public
void
onDestroy() {
// Service停止的时候,注销BroadcastReceiver
unregisterReceiver(ServiceBC);
System.out.println(
"onDestroy"
);
super
.onDestroy();
}
}
|
一个继承BroadcastReceiver的类MyBroadcast:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public
class
MyBroadCast
extends
BroadcastReceiver{
// 定义两个动作,命名规则:“包名.intent.action.动作名称”
public
static
final
String STA =
"com.edu.usingbc.intent.action.STA"
;
public
static
final
String ATS =
"com.edu.usingbc.intent.action.ATS"
;
@Override
public
void
onReceive(Context context, Intent intent) {
// 判断是谁给谁发广播
if
(intent.getAction() == STA) {
System.out.println(
"Service Send a BroadCast to Activity!"
);
}
else
if
(intent.getAction() == ATS) {
System.out.println(
"Actiity Send a BroadCast to Service!"
);
}
}
}
|
Activity_main.xml:
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
|
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
tools:context=
"com.edu.usingbcagains.MainActivity"
>
<Button
android:id=
"@+id/btnActivitySendBC"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"ActivitySendBroadCast"
/>
<Button
android:id=
"@+id/btnServiceSendBC"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"ServiceSendBroadCast"
/>
<Button
android:id=
"@+id/btnRegBC"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"RegBC"
/>
<Button
android:id=
"@+id/btnUnRegBC"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:text=
"UnRegBC"
/>
</LinearLayout>
|
下面是程序运行截图:
测试:首先点击RegBC按钮,注册广播,然后点击ActivitySendBC按钮,可以看到Logcat输出信息为:Actiity Send a BroadCast to Service,点击UnRegBC按钮注销掉Activity的Broadcast。点击ServiceSendBC,可以看到Logcat输出信息为:Service Send a BroadCast to Activity。
小结
注册一个动态的BroadcastReceiver步骤:
registerReceiver(ActivityBC, filter); // ActivityBC为一个Broadcast的实例对象 , filter为一个动作
sendBroadcast(intent);// intent为一个动作,向哪个动作发送广播
最后就是在onReceive()方法中进行接收广播了。(所有的广播都在这个方法中,判断Action,可以分别出不同广播)