一、BroadcastReceiver的简介

用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、广播接收者(BroadcastReceiver)用于异步接收广播Intent,广播Intent的发送是通过调用Context.sendBroadcast()、Context.sendOrderedBroadcast()或者Context.sendStickyBroadcast()来实现的。通常一个广播Intent可以被订阅了此Intent的多个广播接收者所接收,广播接收者和JMS中的Topic消息接收者很相似.

广播接收器只能接收广播,对广播的通知做出反应,很多广播都产生于系统代码.如:时区改变的通知,电池电量不足、用户改变了语言偏好或者开机启动等.

广播接收器没有用户界面,但是,它可以为它们接收到信息启动一个Activity或者使用NotificationManager来通知用户.

二、BroadcastReceiver的两种注册方式

1)静态注册

静态注册方式是在AndroidManifest.xml的application里面定义receiver并设置要接收的action

首先,新建一个Android项目--->取名BroadcastReceiverDemo01

BroadcastActivity.java的代码

            
  1. public class BroadcastActivity extends Activity { 
  2.      
  3.     private Button send = null
  4.      
  5.     @Override 
  6.     public void onCreate(Bundle savedInstanceState) { 
  7.         super.onCreate(savedInstanceState); 
  8.         setContentView(R.layout.main); 
  9.         send = (Button)findViewById(R.id.sned); 
  10.         send.setOnClickListener(new BroadcastListener()); 
  11.     } 
  12.      
  13.     class BroadcastListener implements OnClickListener{ 
  14.  
  15.         @Override 
  16.         public void onClick(View v) { 
  17.             // TODO Auto-generated method stub 
  18.             System.out.println("------------"); 
  19.             Intent intent = new Intent(); 
  20.             intent.setAction(Intent.ACTION_EDIT); 
  21.             BroadcastActivity.this.sendBroadcast(intent); 
  22.         } 
  23.          
  24.     }      
  25.      

TestReceiver.java的代码

            
  1. public class TestReceiver extends BroadcastReceiver { 
  2.      
  3.     public TestReceiver() { 
  4.         System.out.println("TestReceiver create......"); 
  5.     } 
  6.  
  7.     @Override 
  8.     public void onReceive(Context arg0, Intent arg1) { 
  9.         System.out.println("receive......"); 
  10.     } 
  11.  

main.xml的布局文件

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. "http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="wrap_content"  
  9.     android:text="@string/hello" 
  10.     /> 
  11.      
  12.     
  13.         android:id="@+id/sned" 
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/send" 
  17.     /> 
  18.  

strings.xml

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3.     "hello">Hello World, TestReceiver! 
  4.     "app_name">broadcast 
  5.     "send">发送 
  6.  

AndroidManifest.xml

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. "http://schemas.android.com/apk/res/android" 
  3.       package="com.gem.activity" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"
  6.     "@drawable/icon" android:label="@string/app_name"
  7.         ".BroadcastActivity" 
  8.                   android:label="@string/app_name"
  9.              
  10.                 "android.intent.action.MAIN" /> 
  11.                 "android.intent.category.LAUNCHER" /> 
  12.              
  13.          
  14.          
  15.          
  16.         ".TestReceiver"
  17.              
  18.                 "android.intent.action.EDIT"
  19.              
  20.          
  21.      
  22.     "8" /> 
  23.  
  24.   

效果图

当用户点击发送的时候,程序会调用onReceive()方法

2)动态注册

动态注册方式在activity里面调用函数来注册,和静态的内容差不多。一个形参是receiver,另一个是IntentFilter,其中里面是要接收的action.

首先,新建一个Android项目--->取名BroadcastReceiverDemo02

BroadcastActivity.java的代码

            
  1. public class BroadcastActivity extends Activity { 
  2.     private Button send; 
  3.     private Button registerReceiver; 
  4.     private Button unregisterReceiver; 
  5.     private MyReceiver myReceiver; 
  6.      
  7.     @Override 
  8.     public void onCreate(Bundle savedInstanceState) { 
  9.         super.onCreate(savedInstanceState); 
  10.         setContentView(R.layout.main); 
  11.          
  12.         send = (Button)findViewById(R.id.send);         
  13.         send.setOnClickListener(new SendButtontListener()); 
  14.          
  15.         registerReceiver = (Button)findViewById(R.id.registerReceiver); 
  16.         registerReceiver.setOnClickListener(new RegisterReceiverButtonListener()); 
  17.          
  18.         unregisterReceiver = (Button)findViewById(R.id.unregisterReceiver); 
  19.         unregisterReceiver.setOnClickListener(new UnregisterReceiverButtonListener()); 
  20.          
  21.     } 
  22.      
  23.     class SendButtontListener implements OnClickListener{ 
  24.  
  25.         @Override 
  26.         public void onClick(View v) { 
  27.             Intent intent = new Intent(); 
  28.             intent.setAction("Intent.ACTION_EDIT"); 
  29.             BroadcastActivity.this.sendBroadcast(intent); 
  30.             System.out.println("send-----"); 
  31.         } 
  32.          
  33.     } 
  34.      
  35.     class RegisterReceiverButtonListener implements OnClickListener{ 
  36.          
  37.         @Override 
  38.         public void onClick(View v) { 
  39.             myReceiver = new MyReceiver(); 
  40.             IntentFilter filter = new IntentFilter(); 
  41.             filter.addAction("Intent.ACTION_EDIT"); 
  42.             //动态注册BroadcastReceiver 
  43.             registerReceiver(myReceiver, filter); 
  44.         } 
  45.          
  46.     } 
  47.      
  48.     class UnregisterReceiverButtonListener implements OnClickListener{ 
  49.         @Override 
  50.         public void onClick(View v) { 
  51.             //注销BroadcastReceiver  
  52.             unregisterReceiver(myReceiver);  
  53.             System.out.println("close-----"); 
  54.         } 
  55.     } 
  56.      
  57.      

MyReceiver.java的代码

            
  1. public class MyReceiver extends BroadcastReceiver { 
  2.  
  3.     @Override 
  4.     public void onReceive(Context context, Intent intent) {  
  5.             System.out.println("onReceive......"); 
  6.     } 
  7.  

main.xml的布局文件

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. "http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     android:layout_width="fill_parent"  
  8.     android:layout_height="wrap_content"  
  9.     android:text="@string/hello" 
  10.     /> 
  11.      
  12.     
  13.         android:id="@+id/send" 
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="@string/send" 
  17.     /> 
  18.     
  19.         android:id="@+id/registerReceiver" 
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="@string/registerReceiver" 
  23.     /> 
  24.     
  25.         android:id="@+id/unregisterReceiver" 
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="wrap_content"  
  28.         android:text="@string/unregisterReceiver" 
  29.     /> 
  30.  

strings.xml

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3.     "hello">Hello World, TestReceiver! 
  4.     "app_name">broadcast 
  5.     "send">发送广播 
  6.     "registerReceiver">注册广播接收器 
  7.     "unregisterReceiver">注销广播接收器 
  8.  

AndroidManifest.xml

            
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. "http://schemas.android.com/apk/res/android" 
  3.       package="com.gem.activity" 
  4.       android:versionCode="1" 
  5.       android:versionName="1.0"
  6.     "@drawable/icon" android:label="@string/app_name"
  7.         ".BroadcastActivity" 
  8.                   android:label="@string/app_name"
  9.              
  10.                 "android.intent.action.MAIN" /> 
  11.                 "android.intent.category.LAUNCHER" /> 
  12.              
  13.          
  14.          
  15.      
  16.     "8" /> 
  17.  
  18.  

效果图


当用户点击发送的时候,程序没有注册BroadcastReceiver,当用户点击注册广播接收器之后在点击发送会调用MyReceiver中的onReceive()方法,当用户点击注销广播接收器之后程序执行unregisterReceiver()方法

 

 

更多相关文章

  1. Android之记住密码与自动登陆实现
  2. Android(安卓)permission 权限类及中英文说明
  3. Android实现拨打电话的两种方式
  4. Android(安卓)SELinux
  5. Android(安卓)BroadcastReceiver小结
  6. Activity学习(一):生命周期
  7. Android(安卓)逐帧动画isRunning 一直返回true的问题
  8. android用户界面之GridView教程实例汇总
  9. Android:Xmpp协议的简单分析

随机推荐

  1. android gallery 自定义边框+幻灯片
  2. Android之手机号码格式的正则表达式
  3. Android(安卓)悬浮窗的实现源码
  4. ConstraintLayout学习笔记
  5. fragment UI
  6. AudioFlinger 如何通过 hwBinder 调用 Au
  7. sqlite 基本操作
  8. Android(安卓)GreenDao最的基本配置与初
  9. Android实现简单的城市列表功能
  10. Android的应用程序的异常处理2