您的位置:首页 > 移动开发 > Android开发

android的广播broadcast和receiver

2014-02-07 14:02 197 查看
(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
plaincopy

public void onCreate(Bundle savedInstanceState) {  

    super.onCreate(savedInstanceState);  

    setContentView(R.layout.activity_main);          

    Button btn =(Button)findViewById(R.id.callphone);        

      

    btn.setOnClickListener(new Button.OnClickListener(){  

        public void onClick(View v){  

            Intent intent = new Intent();  

            intent.setAction("comzhang");  //intent filter的名字  

            intent.putExtra("name", "cheng");   //Intent中可以“键-值”的格式带数据  

            MainActivity.this.sendBroadcast(intent);  

            Toast.makeText(getApplicationContext(), "send broadcast success", Toast.LENGTH_LONG).show(); //toast方式显示结果  

        }  

    });        

}  

       receiver代码:

[java] view
plaincopy

public class Receiver extends BroadcastReceiver {

    @Override  

    public void onReceive(Context context, Intent intent) {  

        // TODO Auto-generated method stub  

        String name = intent.getExtras().getString("name");  //从intent中取出要带的参数,getString去处键对应的值  

        Log.i("zhangcheng","get :"+name);  

    }  

}  

       andoid menifest的添加内容:

[html] view
plaincopy

<receiver android:name=".Receiver"      //receiver文件名  

    android:exported="false">     

    <intent-filter>     

        <action android:name="comzhang"/>    //匹配的Intent action名    

    </intent-filter>     

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
plaincopy

<receiver android:name=".smsReceiver">  

     <intent-filter android:priority="1000">  

     <action android:name="android.provider.Telephony.SMS_RECEIVED"/>  

     </intent-filter>  

</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.csdn.net/prince58/article/details/6237792
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: