【Android】BroadCast广播机制应用与实例

本文详细介绍如何在Android中创建和使用广播接收器,包括静态和动态注册的方法,并提供了一个完整的示例,展示了如何定义广播接收器类、注册以及发送广播。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何编写广播接收器

第一步:需要继承BroadcastReceiver类,覆写其中的onReceive()方法. 

[java]  view plain copy
  1. class MyBroadcastReceiver extends BroadcastReceiver {  
  2.      //接收到广播会被自动调用    
  3.     @Override  
  4.     public void onReceive (Context context, Intent intent) {  
  5.         //从Intent中获取action  
  6.         …your code here…  
  7.     }  
  8. }  

第二步:定义好广播接收器还不行,必须向系统注册以便让其知道该接收器可以处理哪些广播事件。

常见方式是采用静态注册,修改MENIFEST.xml文件, 在<application></application>中加入receiver标签.

[html]  view plain copy
  1. <application>  
  2.     <activity name=""/>  
  3.     <receiver android:name=".MyBroadcastReceiver">  
  4.         <!-- intent过滤器,指定可以匹配哪些intent, 一般需要定义action 可以是自定义的也可是系统的 -->   
  5.         <intent-filter>  
  6.             <action android:name="com.app.bc.test"/>  
  7.         </intent-filter>  
  8.     </receiver>  
  9. </application>  

第三步:此时我们可以发送一个广播事件出去,代码如下:

[java]  view plain copy
  1. Intent intent = new Intent(“com.app.bc.test”);  
  2. sendBroadcast(intent);//发送广播事件  

动态注册广播接收器

在某个Activity中,我们可以用代码来实现动态注册:
[java]  view plain copy
  1. //生成一个BroadcastReceiver对象  
  2. SMSReceiver  smsReceiver = new SMSReceiver();  
  3. //生成一个IntentFilter对象  
  4. IntentFilter filter = new IntentFilter();         
  5. filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);  
  6. //将BroadcastReceiver对象注册到系统当中  
  7. //此处表示该接收器会处理短信事件  
  8. TestBC1Activity.this.registerReceiver(smsReceiver, filter);   

静态注册和动态注册的区别

1)静态注册:在AndroidManifest.xml注册,android不能自动销毁广播接收器,也就是说当应用程序关闭后,还是会接收广播。
2)动态注册:在代码中通过registerReceiver()手工注册.当程序关闭时,该接收器也会随之销毁。当然,也可手工调用unregisterReceiver()进行销毁。

操作小结

静态注册的步骤:
定义广播接收器,继承BroadcastReceiver类,覆写onReceive函数.
在xml文件中注册监听器,定义Intent-Filter中感兴趣的action操作.
使用sendBroadCast向系统发送对其感兴趣的广播接收器中.
动态注册的步骤:
[java]  view plain copy
  1. SMSReceiver  smsReceiver = new SMSReceiver();  
  2. IntentFilter filter = new IntentFilter();         
  3. filter.addAction(“android.provider.Telephony.SMS_RECEIVED”);  
  4. TestBC1Activity.this.registerReceiver(smsReceiver, filter);   

(无需在配置文件中注册接收器)

应用实例

[java]  view plain copy
  1. package com.app.test02;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.IntentFilter;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9.   
  10. public class BroadCastActivity1 extends Activity{  
  11.     Intent intent = new Intent();  
  12.     BroadCastTest1 bCastTest1 = new BroadCastTest1();  
  13. //  BroadCastTest11 bCastTest11 = new BroadCastTest11();  
  14. //  BroadCastTest111 bCastTest111 = new BroadCastTest111();  
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         // TODO Auto-generated method stub  
  18.         super.onCreate(savedInstanceState);  
  19.           
  20.         setContentView(R.layout.activity_bc1);  
  21.         //静态注册  
  22.         findViewById(R.id.button1).setOnClickListener(new OnClickListener() {  
  23.             @Override  
  24.             public void onClick(View v) {  
  25.                 // TODO Auto-generated method stub  
  26.                 intent.setAction("bc.test101");  
  27.                 intent.putExtra("name""静态的");  
  28.                 sendBroadcast(intent);  
  29. //              sendOrderedBroadcast(intent, null);  
  30.             }  
  31.         });  
  32.         //动态注册  
  33.         findViewById(R.id.button2).setOnClickListener(new OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View v) {  
  36.                 // TODO Auto-generated method stub  
  37.                   
  38.                 IntentFilter intentFilter = new IntentFilter();  
  39.                 intentFilter.addAction("bc.test102");  
  40.   
  41.                 BroadCastActivity1.this.registerReceiver(bCastTest1, intentFilter);  
  42.                   
  43.                 intent.setAction("bc.test102");  
  44.                 intent.putExtra("name""动态的");  
  45.                 sendBroadcast(intent);  
  46. //              sendOrderedBroadcast(intent, null);  
  47.             }  
  48.         });  
  49.   
  50.         findViewById(R.id.button3).setOnClickListener(new OnClickListener() {  
  51.             @Override  
  52.             public void onClick(View v) {  
  53.                 // TODO Auto-generated method stub  
  54.                 unregisterReceiver(bCastTest1);  
  55.                 finish();  
  56.             }  
  57.         });  
  58.     }  
  59. }  

广播类
[java]  view plain copy
  1. package com.app.test02;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.widget.Toast;  
  7.   
  8. public class BroadCastTest1 extends BroadcastReceiver{  
  9.     @Override  
  10.     public void onReceive(Context context, Intent intent) {  
  11.         // TODO Auto-generated method stub  
  12.         String name = intent.getStringExtra("name");  
  13.           
  14.         Toast.makeText(context, "广播1:" + name, 1000).show();  
  15.         System.out.println(1);  
  16.     }  
  17. }  

AndroidManifest.xml
[html]  view plain copy
  1. <receiver android:name=".BroadCastTest1">  
  2.     <intent-filter android:priority="3">  
  3.         <action android:name="bc.test101"/>  
  4.     </intent-filter>  
  5. </receiver>  

布局文件
[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"   
  6.     android:background="#fff"  
  7.     android:padding="10dp">  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical"  
  13.         android:gravity="center_horizontal" >  
  14.   
  15.         <Button  
  16.             android:id="@+id/button1"  
  17.             android:layout_width="match_parent"  
  18.             android:layout_height="wrap_content"  
  19.             android:text="静态注册广播" />  
  20.   
  21.         <Button  
  22.             android:id="@+id/button2"  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:text="动态注册广播" />  
  26.   
  27.         <Button  
  28.             android:id="@+id/button3"  
  29.             android:layout_width="match_parent"  
  30.             android:layout_height="wrap_content"  
  31.             android:text="退出" />  
  32.   
  33.     </LinearLayout>  
  34.   
  35. </LinearLayout>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值