Android之BroadcastReceiver的使用

来源:互联网 发布:c语言指针编程题 编辑:程序博客网 时间:2024/05/21 16:23
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://emilyzhou.blog.51cto.com/3632647/685387

一、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. <LinearLayout xmlns:android="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. <TextView   
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/hello" 
  11.     /> 
  12.      
  13.     <Button 
  14.         android:id="@+id/sned" 
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/send" 
  18.     /> 
  19. </LinearLayout> 

strings.xml

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

AndroidManifest.xml

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

效果图

当用户点击发送的时候,程序会调用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. <LinearLayout xmlns:android="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. <TextView   
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="wrap_content"  
  10.     android:text="@string/hello" 
  11.     /> 
  12.      
  13.     <Button 
  14.         android:id="@+id/send" 
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/send" 
  18.     /> 
  19.     <Button 
  20.         android:id="@+id/registerReceiver" 
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:text="@string/registerReceiver" 
  24.     /> 
  25.     <Button 
  26.         android:id="@+id/unregisterReceiver" 
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="@string/unregisterReceiver" 
  30.     /> 
  31. </LinearLayout> 

strings.xml

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

AndroidManifest.xml

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

效果图


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

 

 

本文出自 “Pain past is pleasure” 博客,请务必保留此出处http://emilyzhou.blog.51cto.com/3632647/685387

原创粉丝点击