静态注册:静态注册是在AndroidManifest.xml文件中配置的html
<receiver android:name=".MyReceiver"> <intent-filter> <action android:name="android.intent.action.MY_BROADCAST"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>
配置了以上信息以后,只要是android.intent.action.MY_BROADCAST这个地址的广播,MyReceiver都可以接收的到。android
属于常驻型的,也就是说当应用关闭后,若是有广播信息传来,MyReceiver也会被系统调用而自动运行。spa
动态注册:在代码中动态的指定广播地址并注册,一般咱们是在Activity或Service注册一个广播。.net
通常:在onStart中注册,在onStop/onDestroy中取消unregisterReceivercode
/** * 注册 */ IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(String); //为BroadcastReceiver指定action,使之用于接收同action的广播 context.registerReceiver(BroadcastReceiver, intentFilter);
/** * 取消 */ unregisterReceiver(rhelper);
区别:xml
1.动态注册较静态注册灵活。htm
2.当静态注册一个BroadcastReceiver时,不论应用程序是启动与否。均可以接收对应的广播。 blog
3.动态注册的时候,若是不执行unregisterReceiver();方法取消注册,跟静态是同样的。get
4.可是当动态注册若是执行unregisterReceiver();方法取消注册,就不能接收广播了。it
二.广播的发送
指定广播目标Action:Intent intent = new Intent(actionString);
而且可经过Intent携带消息 :intent.putExtra("msg", "hi,我经过广播发送消息了");
发送广播消息:Context.sendBroadcast(intent )
三.广播的接收
四.广播的分类
1.http://blog.csdn.net/liuhe688/article/details/6955668/
http://www.cnblogs.com/lwbqqyumidi/p/4168017.html
http://yangguangfu.iteye.com/blog/1063732
http://www.cnblogs.com/jico/articles/1838293.html