android broadcast

来源:互联网 发布:网络舆情内参征订 编辑:程序博客网 时间:2024/04/28 17:13

普通广播(Normal Broadcast):

一,优缺点:和有序广播的优缺点相反!

二,发送广播的方法:sendBroadcast()


有序广播(Ordered Broadcast):

一,优缺点

优点:1,按优先级的不同,优先Receiver可对数据进行处理,并传给下一个Receiver

2,通过abortBroadcast可终止广播的传播

缺点:效率低

二,发送广播的方法:sendOrderedBroadcast()

三,优先接收到Broadcast的Receiver可通过setResultExtras(Bundle)方法将处理结果存入Broadcast中,

下一个Receiver 通过 Bundle bundle=getResultExtras(true)方法获取上一个 Receiver传来的数据

程序效果:点击按钮,两个Receiver接收同一条广播,在logcat中打印出数据(按照Receiver的优先顺序,Receiver2先,Receiver1后)

Java代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.song"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.         <activity  
  13.             android:label="@string/app_name"  
  14.             android:name=".C48_BroadcastActivity" >  
  15.             <intent-filter >  
  16.                 <action android:name="android.intent.action.MAIN" />  
  17.   
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.             <!--优先级的设定 MyReceiver2大于MyReceiver1,优先级的范围-1000~1000 -->  
  21.         </activity>  
  22.         <receiver android:name=".MyReceiver1">  
  23.             <intent-filter android:priority="200">  
  24.                 <action android:name="com.song.123"/>  
  25.             </intent-filter>  
  26.         </receiver>  
  27.         <receiver android:name=".MyReceiver2">  
  28.             <intent-filter android:priority="1000">  
  29.                 <action android:name="com.song.123"/>  
  30.             </intent-filter>  
  31.         </receiver>  
  32.     </application>  
  33.   
  34. </manifest>  

 

Java代码  收藏代码
  1. package com.song;  
  2. //发送广播,bundle绑上key为a的数据  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class C48_BroadcastActivity extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.     Button button;  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         button=(Button)findViewById(R.id.button);  
  18.         button.setOnClickListener(new OnClickListener() {  
  19.               
  20.             @Override  
  21.             public void onClick(View v) {  
  22.                 // TODO Auto-generated method stub  
  23.                 Intent intent=new Intent("com.song.123");  
  24.                 Bundle bundle=new Bundle();  
  25.                 bundle.putString("a""aaa");  
  26.                 intent.putExtras(bundle);  
  27.                 //有序广播  
  28.                 sendOrderedBroadcast(intent, null);  
  29.             }  
  30.         });  
  31.           
  32.     }  
  33. }  

 

Java代码  收藏代码
  1. package com.song;  
  2. //优先接到广播,bundle绑上key为b的数据  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7.   
  8. public class MyReceiver2 extends BroadcastReceiver{  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         // TODO Auto-generated method stub  
  13.         System.out.println("receiver2");  
  14. //      context.getSystemService(name);  
  15.         Bundle bundle=intent.getExtras();  
  16.         bundle.putString("b""bbb");  
  17.         System.out.println("a="+bundle.get("a"));  
  18.         setResultExtras(bundle);  
  19.         //切断广播  
  20. //      abortBroadcast();  
  21.     }  
  22.   
  23. }  

 

Java代码  收藏代码
  1. package com.song;  
  2. //接收从receiver2传来的广播,包含key为a和b的数据  
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7.   
  8. public class MyReceiver1 extends BroadcastReceiver{  
  9.   
  10.     @Override  
  11.     public void onReceive(Context context, Intent intent) {  
  12.         // TODO Auto-generated method stub  
  13.         System.out.println("receiver1");  
  14.         //要不要接受上一个广播接收器receiver2传来的的数据  
  15.         Bundle bundle=getResultExtras(true);  
  16.         System.out.println("a="+bundle.getString("a")+",b="+bundle.getString("b"));  
  17.     }  
  18.   
  19. }  
0 0
原创粉丝点击