android的广播broadcast和receiver

(1)基础概念

       广播发送者:通常广播发送方就是调用Context.sendBroadcast()的程序,而广播接收者就是继承BroadcastReceiver的程序。广播发送分两种:

A,同步广播:发送方发出后,几乎同时到达多个广播接收者处,并且无法终止广播继续传播,使用Context.sendBroadcast(intent);。

B,有序广播:广播接收者需要提前设置优先级,优先级高的先接收到广播,优先级数值为-1000~1000,而且能终止广播(abortBroadcast());使用Context.sendOrderedBroadcast(intent);

(2)发送接收模型核心代码
        A,同步广播发送方:

Intent intent = new Intent();   
intent.setAction("...");   
Context.sendBroadcast(intent);  
        B,有序广播发送方:

Intent intent = new Intent();   
intent.setAction("...");   
Context.sendOrderedBroadcast(intent,null);  
        C,广播接收者核心代码:

public class Receiver extends BroadcastReceiver{   
    public void onReceive(Context context, Intent intent) {   
        Bundle bundle = intent.getExtras();   
        ...   
    }   

关于Intent filter的方式有两种:在注册receiver时用addAction,或者在androidmenifest.xml中加上。

(3) 普通广播实例,通过按钮发送一个广播,receiver中接收到后将广播中附带的信息在log中显示。

       activity代码:

[java]  view plain copy
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.activity_main);          
  4.     Button btn =(Button)findViewById(R.id.callphone);        
  5.       
  6.     btn.setOnClickListener(new Button.OnClickListener(){  
  7.         public void onClick(View v){  
  8.             Intent intent = new Intent();  
  9.             intent.setAction("comzhang");  //intent filter的名字  
  10.             intent.putExtra("name""cheng");   //Intent中可以“键-值”的格式带数据  
  11.             MainActivity.this.sendBroadcast(intent);  
  12.             Toast.makeText(getApplicationContext(), "send broadcast success", Toast.LENGTH_LONG).show(); //toast方式显示结果  
  13.         }  
  14.     });        
  15. }  

       receiver代码:

[java]  view plain copy
  1. public class Receiver extends BroadcastReceiver {
  2.     @Override  
  3.     public void onReceive(Context context, Intent intent) {  
  4.         // TODO Auto-generated method stub  
  5.         String name = intent.getExtras().getString("name");  //从intent中取出要带的参数,getString去处键对应的值  
  6.         Log.i("zhangcheng","get :"+name);  
  7.     }  
  8. }  

       andoid menifest的添加内容:

[html]  view plain copy
  1. <receiver android:name=".Receiver"      //receiver文件名  
  2.     android:exported="false">     
  3.     <intent-filter>     
  4.         <action android:name="comzhang"/>    //匹配的Intent action名    
  5.     </intent-filter>     
  6. eceiver>    

执行它就可以在LOG中看到:get :cheng
(4)补充说明
       A,在一个Service中发送广播的话,与在activity中有差别,需要在intent中加上标志setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)。

       B,以上实例是XML加Intent过滤广播的方式,也可以在程序中自定义广播的接收模式:

发送:

Intent startCameraIntent = new Intent("android.intent.action.HTC_START_CAMERA");
mContext.sendBroadcast(startCameraIntent);

接收:

ScreenFilter.addAction("android.intent.action.HTC_START_CAMERA")  //字符串匹配就行
registerReceiver(mScreenFilterReceiver, ScreenFilter);
....................
if(intent.getAction().equals("android.intent.action.HTC_START_CAMERA"))  {

(5) 关于有序广播的说明

        有序广播可以设定接收的优先级,并在接收后删掉这个广播传送。在XML中设定receiver的优先级:

[html]  view plain copy
  1. <receiver android:name=".smsReceiver">  
  2.      <intent-filter android:priority="1000">  
  3.      <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  
  4.      </intent-filter>  
  5. </receiver>  

 优先级别声明在intent-filter 元素的 android:priority 属性中,数越大优先级别越高,取值范围:-1000到1000,优先级别也可以调用IntentFilter对象的setPriority()进行设置。
         有序广播的接收者可以终止广播Intent的传播,广播Intent的传播一旦终止,后面的接收者就无法接收到广播,使用abortBroadcast();即可。

 

参考原文:http://www.eoeandroid.com/thread-14095-1-1.html

参考原文:http://www.linuxidc.com/Linux/2012-07/65943.htm

参考原文:http://blog.youkuaiyun.com/prince58/article/details/6237792

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值