BroadcastReceiver详解(一)

BroadcastReceiver就是一个广播消息接收者。

广播之间信息的传递是通过Intent对象来传递的。

  • Intent调用分为显示调用和隐式调用两种,由于这里能通知到所有的接收者,所以肯定不能利用显示调用,只有利用隐式调用Intent对象了
  • 显式intent是指明确指出此intent是启动哪个activity.
    隐式intent是指并没有指出要启动哪个activity,而要系统自动去判断并启动匹配的activity.

首先构造一个接收器:

      广播的传递是靠Intent的,OnReceive的第二个参数,就是广播传过来的Intent。

  1. public class MyReceiver extends BroadcastReceiver {  
  2.   
  3.     private static final String TAG = "MyReceiver";    
  4.       
  5.     @Override  
  6.     public void onReceive(Context context, Intent intent) {  
  7.         // TODO Auto-generated method stub  
  8.   
  9.         String msg = intent.getStringExtra("msg");    
  10.         Log.i(TAG, "MyReceiver:"+msg);    
  11.           
  12.     }  
  13.   
大家可能会想,就这么着,就能收到广播了?当然不是,上面我们说了,通过隐式Intent来发送广播的,我们肯定要匹配这个Intent啊,匹配Intent的术语是Activity中的,在广播这里,叫要注册,也就是要注册一下,什么样的Intent能接收。

MyReceiver的广播接收 注册代码如下:(静态注册)

  1. <receiver android:name=".MyReceiver">  
  2.     <intent-filter>  
  3.         <action android:name="android.intent.action.MY_BROADCAST"/>  
  4.         <category android:name="android.intent.category.DEFAULT" />  
  5.     </intent-filter>  
  6. </receiver>  
android:name:对应接收器的类名;我们自定义的类名叫MyReceiver ,所以这里写".MyReceiver  "

intent-filter标签里,同样是必须的两项:action和category;我在这里自定义了action的名字,等下隐式发送通过时,就是利用匹配action来接收通知的。

这里特别注意<activity> 标签与<receiver>标签的构造。完全相同!!!!!!!
完全相同体现在:

  • 1、所处位置:都直属<application>标签;
  • 2、参数构造基本一样;都有android:name,都有<intent-filter>;
  • 3、都是通过Intent传递参数,也都是通过Intent进行匹配!!!!

最后是发送广播:

我们在主页面加一个Button,当点击Button时发送广播消息。

布局文件如下:(activity_main.xml)


  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context="com.example.test_brocast_blog.MainActivity" >  
  6.   
  7.    <Button  
  8.         android:id="@+id/sent_btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="发送Broadcast" />  
  12.   
  13. </RelativeLayout>  
代码如下:(MainActivity.java)
  1. public class MainActivity extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_main);  
  7.       
  8.         Button btn= (Button)findViewById(R.id.sent_btn);  
  9.         btn.setOnClickListener(new View.OnClickListener() {  
  10.               
  11.             @Override  
  12.             public void onClick(View v) {  
  13.                 // TODO Auto-generated method stub  
  14.                 send();  
  15.             }  
  16.         });  
  17.           
  18.     }  
  19.       
  20.     public void send() {  
  21.         Intent intent = new Intent("android.intent.action.MY_BROADCAST");  
  22.         intent.putExtra("msg""hello receiver.");  
  23.         sendBroadcast(intent);  
  24.     }  
  25. }  
真正的发送代码在send()函数中,在这个函数中可以看到,我们通过传进去刚才注册的MyRecevier的action,来构造一个隐式Intent,然后利用PutExtra放进去一个额外信息(这个不是必须的,我们仅仅是为了跟踪这个消息传到了哪里去,在《详解Intnent》系统文章中有讲怎样构造一个隐式Intent),与StartActivity不同的是,这里利用的是sendBroadcast(intent)来发送这个Intent;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值