简单摘录有关广播(Broadcast)的重要知识点。
一般广播,所有满足条件的BroadcastReceive都会执行其OnReceive方法,但执行顺序无保证。
顺序广播,发送的Intent(意图)会根据BroadcastReceive注册的IntentFilter设置的优先级的顺序来执行OnReceive(接受)方法。
粘性广播,其发送的Intent在发送后会一直存在,并且在以后调用registerReceiver注册相匹配的Receive时会把这个Intent直接返回给新注册的Receiver。
在清单文件(AndroidManifest.xml)中使用<receiver>标签声明注册,并在标签内使用<intent-filter>标签设置过滤器。
注册使用调用Context对象的registerReceiver方法,销毁调用Context对象的unregisterReceiver,另外动态注册BroadcastReceiver对应的Context销毁时,BroadcastReceiver也就自动取消注册。
额,为了与上面的干货对应,不过有点不伦不类,不过,没事,算博眼球了。
下面会有一个简单的例子介绍静态广播和动态广播,我会摘录点重要代码,具体看我后面的源代码了,大片大片的源代码还是放在IDE(集成开发工具里面好),各位如能一边看我博客,一边看eclipse效果更佳。
1.清单列表(Activity后面添加):
1: <!-- 注册自定义静态广播接收器 -->
2: <receiver android:name="com.example.broadcastexample.StaticReceiver">
3: <intent-filter>
4: <action android:name="com.bn.my.staticreceiver"></action>
5: </intent-filter>
6: </receiver>
2.发布广播:
1: //按钮变量
2: private Button sendStaticButton;
3: private Button sendDynamicButton;
4: //静态变量
5: private static final String STATICACTION="com.bn.my.staticreceiver";
6: private static final String DYNAMICATION="com.bn.my.dynamicreceiver";
7:
8: @Override
9: protected void onCreate(Bundle savedInstanceState) {
10: super.onCreate(savedInstanceState);
11: setContentView(R.layout.activity_main);
12: //得到按钮并注册事件
13: sendStaticButton=(Button)findViewById(R.id.btn_sendStaticBroadcast);
14: sendStaticButton.setOnClickListener(new DiyOnClickLinstener());
15: sendDynamicButton=(Button)findViewById(R.id.btn_sendDynamicBroadcast);
16: sendDynamicButton.setOnClickListener(new DiyOnClickLinstener());
17: }
内部类DiyOnClickLinstener
1: class DiyOnClickLinstener implements OnClickListener{
2:
3: @Override
4: public void onClick(View v) {
5: // TODO Auto-generated method stub
6: Intent intent=new Intent();
7: if(v.getId()==R.id.btn_sendStaticBroadcast){
8: intent.setAction(STATICACTION);
9: intent.putExtra("msg", "接受静态注册广播成功!");
10: }
11: else if(v.getId()==R.id.btn_sendDynamicBroadcast)
12: {
13: intent.setAction(DYNAMICATION);
14: intent.putExtra("msg", "接受动态广播成功!");
15: }
16: //发布广播
17: sendBroadcast(intent);
18: }
19:
20: }
3.接受广播
其实在清单就告诉系统我的接受类是StaticReceiver,该类必须继承BroadcastReceiver,我们可以新建个类。
1: public class StaticReceiver extends BroadcastReceiver {
2:
3: @Override
4: public void onReceive(Context context, Intent intent) {
5: // TODO Auto-generated method stub
6: String msg=intent.getStringExtra("msg");
7: Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
8: }
9:
10: }
1.发布广播,此处代码在静态广播的发布广播处,自行寻找。
2.onStart方法处,注册动态广播,同清单文件里面填写,并指定接受广播时的事件。
注意,程序的运行顺序为onCreate()->onStart()。
1: private BroadcastReceiver dynamciReceiver=/**
2: * @author haichao
3: * 动态添加的接受事件
4: */
5: new BroadcastReceiver(){
6:
7: @Override
8: public void onReceive(Context context, Intent intent) {
9: // TODO Auto-generated method stub
10: if(intent.getAction().equals(DYNAMICATION)){//动作检测
11: String msg=intent.getStringExtra("msg");//获取intentn内容
12: Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();//显示
13: }
14: }
15:
16: };
17: /* (non-Javadoc)
18: * @see android.app.Activity#onStart()
19: * 动态添加广播
20: */
21: @Override
22: protected void onStart() {
23: // TODO Auto-generated method stub
24: super.onStart();
25: //添加意图筛选器
26: IntentFilter dynamic_filter=new IntentFilter();
27: dynamic_filter.addAction(DYNAMICATION);
28: registerReceiver(dynamciReceiver,dynamic_filter);
29: }